Skip to content
Myr

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

1 file+25 added−14 removed

modified:immer-es5.js+25 added-14 removed
2 unmodified lines
3// @ts-check45const PROXY_TARGET = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version6const CHANGED_STATE = Symbol("immer-changed-state")7const PARENT = Symbol("immer-parent")89let autoFreeze = true1011/**
11 unmodified lines
23 let finished = false24 const descriptors = {}2526 // 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 }3738 function assertUnfinished() {39 if (finished)
1 unmodified line
41 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?"42 )43 }4439 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: value
17 unmodified lines
76 // 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 lines
100 }101102 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 }109110 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 lines
168 return freeze(proxy.map(finalize))169 }170171 // 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 lines
186 finished = true187 return res188}189190function markParentsDirty(proxy) {191 let parent = proxy192 while ((parent = parent[PARENT])) {193 if (parent[CHANGED_STATE]) return194 parent[CHANGED_STATE] = true195 }196}197198function isPlainObject(value) {199 if (value === null || typeof value !== "object") return false200 const proto = Object.getPrototypeOf(value)201 return proto === Object.prototype || proto === null
61 unmodified lines