introduce parent concept
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent ec41f11 · Browse files · 1 file·+25 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−14 removed
- other:+25 added−14 removed
Provenance
- Commit
cf78d6d- Tree
ee36d55- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
ec41f11
1 file changed+25 added−14 removed
modified:immer-es5.js+25 added-14 removed
2 unmodified lines3 // @ts-check4 5 const PROXY_TARGET = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version6 const CHANGED_STATE = Symbol("immer-changed-state")7+const PARENT = Symbol("immer-parent")8 9 let autoFreeze = true10 11 /**11 unmodified lines23 let finished = false24 const descriptors = {}25 26 // creates a proxy for plain objects / arrays26- function createProxy(base) {27- if (isPlainObject(base)) return createObjectProxy(base)28- if (Array.isArray(base)) return createArrayProxy(base)29- throw new Error("Expected a plain object or array")27+ function createProxy(base, parent) {28+ let proxy29+ if (isPlainObject(base)) proxy = createObjectProxy(base)30+ else if (Array.isArray(base)) proxy = createArrayProxy(base)31+ else throw new Error("Expected a plain object or array")32+ createHiddenProperty(proxy, PROXY_TARGET, base)33+ createHiddenProperty(proxy, CHANGED_STATE, false)34+ createHiddenProperty(proxy, PARENT, parent)35+ return proxy36 }37 38 function assertUnfinished() {39 if (finished)1 unmodified line41 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?"42 )43 }44 39- function proxySet(target, prop, value) {45+ function proxySet(proxy, prop, value) {46 // immer func not ended?47 assertUnfinished()48 // actually a change?43- if (Object.is(target[prop], value)) return49+ if (Object.is(proxy[prop], value)) return50 // mark changed45- target[CHANGED_STATE] = true51+ proxy[CHANGED_STATE] = true52+ markParentsDirty(proxy)53 // and stop proxying, we know this object has changed47- Object.defineProperty(target, prop, {54+ Object.defineProperty(proxy, prop, {55 enumerable: true,56 writable: true,57 configurable: true,58 value: value17 unmodified lines76 // if not proxy-able, return value77 if (!isPlainObject(value) && !Array.isArray(value))78 return value79 // otherwise, create proxy73- const proxy = createProxy(value)80+ const proxy = createProxy(value, this)81 // and make sure this proxy is returned from this prop in the future if read82 // (write behavior as is)83 Object.defineProperty(this, prop, {84 configurable: true,15 unmodified lines100 }101 102 function createObjectProxy(base) {103 const proxy = {}97- createHiddenProperty(proxy, PROXY_TARGET, base)98- createHiddenProperty(proxy, CHANGED_STATE, false)104 Object.keys(base).forEach(prop =>105 Object.defineProperty(proxy, prop, createPropertyProxy(prop))106 )107 return proxy108 }109 110 function createArrayProxy(base) {111 const proxy = []107- createHiddenProperty(proxy, PROXY_TARGET, base)108- createHiddenProperty(proxy, CHANGED_STATE, false)112 for (let i = 0; i < base.length; i++)113 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))114 return proxy115 }52 unmodified lines168 return freeze(proxy.map(finalize))169 }170 171 // create proxy for root169- const rootClone = createProxy(baseState)172+ const rootClone = createProxy(baseState, undefined)173 // execute the thunk174 const maybeVoidReturn = thunk(rootClone)175 //values either than undefined will trigger warning;176 !Object.is(maybeVoidReturn, undefined) &&9 unmodified lines186 finished = true187 return res188 }189 190+function markParentsDirty(proxy) {191+ let parent = proxy192+ while ((parent = parent[PARENT])) {193+ if (parent[CHANGED_STATE]) return194+ parent[CHANGED_STATE] = true195+ }196+}197+198 function isPlainObject(value) {199 if (value === null || typeof value !== "object") return false200 const proto = Object.getPrototypeOf(value)201 return proto === Object.prototype || proto === null61 unmodified lines