proxy.js
This file
Path
src/proxy.js
157 lines · 4.0 KiB · JavaScript
Last changed by
Removed some duplicate codeMichel Weststratebefore adoptionee544cc
History of this fileTake it
Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 1–3why, one step back1"use strict"2// @ts-check3Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 4why, one step back4import {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
line 5why, one step back5 is,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 6–9why, one step back6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–12why, one step back10 finalize,11 shallowCopy,12 verifyReturnValue,Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
lines 13–14why, one step back13 each,14 finalizeNonProxiedObjectFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 15why, one step back15} from "./common"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 16why, one step back16Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 17why, one step back17let proxies = nullMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 18–36why, one step back1819const objectTraps = {20 get,21 has(target, prop) {22 return prop in source(target)23 },24 ownKeys(target) {25 return Reflect.ownKeys(source(target))26 },27 set,28 deleteProperty,29 getOwnPropertyDescriptor,30 defineProperty,31 setPrototypeOf() {32 throw new Error("Don't even try this...")33 }34}3536const arrayTraps = {}Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 37why, one step back37each(objectTraps, (key, fn) => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 38–39why, one step back38 arrayTraps[key] = function() {39 arguments[0] = arguments[0][0]Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 40why, one step back40 return fn.apply(this, arguments)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 41why, one step back41 }Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 42why, one step back42})Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 43–62why, one step back4344function createState(parent, base) {45 return {46 modified: false,47 finalized: false,48 parent,49 base,50 copy: undefined,51 proxies: {}52 }53}5455function source(state) {56 return state.modified === true ? state.copy : state.base57}5859function get(state, prop) {60 if (prop === PROXY_STATE) return state61 if (state.modified) {62 const value = state.copy[prop]Fixed bug where new objects where proxiedMichel Weststratebefore adoption4ad622f
lines 63–65why, one step back63 if (value === state.base[prop] && isProxyable(value))64 // only create proxy if it is not yet a proxy, and not a new object65 // (new objects don't need proxying, they will be processed in finalize anyway)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 66–79why, one step back66 return (state.copy[prop] = createProxy(state, value))67 return value68 } else {69 if (prop in state.proxies) return state.proxies[prop]70 const value = state.base[prop]71 if (!isProxy(value) && isProxyable(value))72 return (state.proxies[prop] = createProxy(state, value))73 return value74 }75}7677function set(state, prop, value) {78 if (!state.modified) {79 if (Added polyfill for Object.is, fixes #77Michel Weststratebefore adoption25d7274
line 80why, one step back80 (prop in state.base && is(state.base[prop], value)) ||Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 81–114why, one step back81 (prop in state.proxies && state.proxies[prop] === value)82 )83 return true84 markChanged(state)85 }86 state.copy[prop] = value87 return true88}8990function deleteProperty(state, prop) {91 markChanged(state)92 delete state.copy[prop]93 return true94}9596function getOwnPropertyDescriptor(state, prop) {97 const owner = state.modified98 ? state.copy99 : prop in state.proxies ? state.proxies : state.base100 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)101 if (descriptor && !(Array.isArray(owner) && prop === "length"))102 descriptor.configurable = true103 return descriptor104}105106function defineProperty() {107 throw new Error(108 "Immer does currently not support defining properties on draft objects"109 )110}111112function markChanged(state) {113 if (!state.modified) {114 state.modified = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 115–116why, one step back115 state.copy = shallowCopy(state.base)116 // copy the proxies over the base-copyMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 117–124why, one step back117 Object.assign(state.copy, state.proxies) // yup that works for arrays as well118 if (state.parent) markChanged(state.parent)119 }120}121122// creates a proxy for plain objects / arrays123function createProxy(parentState, base) {124 const state = createState(parentState, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 125–128why, one step back125 const proxy = Array.isArray(base)126 ? Proxy.revocable([state], arrayTraps)127 : Proxy.revocable(state, objectTraps)128 proxies.push(proxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 129–131why, one step back129 return proxy.proxy130}131Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 132why, one step back132export function finalizeObject(state) {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 133–134why, one step back133 const copy = state.copy134 const base = state.baseMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 135–137why, one step back135 each(copy, (prop, value) => {136 if (value !== base[prop]) copy[prop] = finalize(value)137 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 138–141why, one step back138 return freeze(copy)139}140141export function produceProxy(baseState, producer) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 142–143why, one step back142 const previousProxies = proxies143 proxies = []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 144–147why, one step back144 try {145 // create proxy for root146 const rootClone = createProxy(undefined, baseState)147 // execute the thunkMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 148why, one step back148 verifyReturnValue(producer(rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 149–151why, one step back149 // and finalize the modified proxy150 const res = finalize(rootClone)151 // revoke all proxiesMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 152why, one step back152 each(proxies, (_, p) => p.revoke())Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 153–154why, one step back153 return res154 } finally {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 155why, one step back155 proxies = previousProxiesMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 156–157why, one step back156 }157}