Skip to content
Myr

Optimize change detection by tracking parents

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 cf78d6d · Browse files · 1 file+25 added−27 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−27 removed

  • other:+25 added−27 removed

1 file+25 added−27 removed

modified:immer-es5.js+25 added-27 removed
20 unmodified lines
21function immer(baseState, thunk) {22 let finalizing = false23 let finished = false24 const descriptors = {}25 const proxies = []2627 // creates a proxy for plain objects / arrays28 function createProxy(base, parent) {29 let proxy
2 unmodified lines
32 else throw new Error("Expected a plain object or array")33 createHiddenProperty(proxy, PROXY_TARGET, base)34 createHiddenProperty(proxy, CHANGED_STATE, false)35 createHiddenProperty(proxy, PARENT, parent)36 proxies.push(proxy)37 return proxy38 }3940 function assertUnfinished() {
8 unmodified lines
49 assertUnfinished()50 // actually a change?51 if (Object.is(proxy[prop], value)) return52 // mark changed51 proxy[CHANGED_STATE] = true52 markParentsDirty(proxy)53 markDirty(proxy)54 // and stop proxying, we know this object has changed55 Object.defineProperty(proxy, prop, {56 enumerable: true,57 writable: true,
59 unmodified lines
117118 // this sounds very expensive, but actually it is not that extensive in practice119 // as it will only visit proxies, and only do key-based change detection for objects for120 // which it is not already know that they are changed (that is, only object for which no known key was changed)120 function markChanges(proxy) {121 if (!isProxy(proxy)) return false122 if (isPlainObject(proxy)) return markObjectChanges(proxy)123 if (Array.isArray(proxy)) return markArrayChanges(proxy)121 function markChanges() {122 // intentionally we process the proxies in reverse order;123 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore124 // reverse order of proxy creation approximates this125 for (let i = proxies.length - 1; i >= 0; i--) {126 const proxy = proxies[i]127 if (128 proxy[CHANGED_STATE] === false &&129 ((isPlainObject(proxy) && hasObjectChanges(proxy)) ||130 (Array.isArray(proxy) && hasArrayChanges(proxy)))131 ) {132 markDirty(proxy)133 }134 }135 }136126 function markObjectChanges(proxy) {137 function hasObjectChanges(proxy) {138 const baseKeys = Object.keys(proxy[PROXY_TARGET])139 const keys = Object.keys(proxy)129 let hasChanges = proxy[CHANGED_STATE]130 // look deeper, this object was not modified, but maybe some of its children are131 for (let i = 0; i < keys.length; i++) {132 if (markChanges(proxy[keys[i]])) hasChanges = true133 }134 // all children are the same, but maybe there are items added / removed135 if (!hasChanges) hasChanges = !shallowEqual(baseKeys, keys)140 return !shallowEqual(baseKeys, keys)136 return (proxy[CHANGED_STATE] = hasChanges)141 }142139 function markArrayChanges(proxy) {140 const target = proxy[PROXY_TARGET]141 let hasChanges = proxy[CHANGED_STATE]142 for (let i = 0; i < proxy.length; i++) {143 if (markChanges(proxy[i])) hasChanges = true144 }145 // all children are the same, but maybe there are items added / removed146 if (!hasChanges) hasChanges = target.length !== proxy.length147 return (proxy[CHANGED_STATE] = hasChanges)143 function hasArrayChanges(proxy) {144 return proxy[PROXY_TARGET].length !== proxy.length145 }146147 function finalize(proxy) {148 // given a base object, returns it if unmodified, or return the changed cloned if modified
27 unmodified lines
176 )177 // and finalize the modified proxy178 finalizing = true179 // find and mark all changes (for parts not done yet)183 markChanges(rootClone)180 markChanges()181 const res = finalize(rootClone)182 // make sure all proxies become unusable183 finished = true184 return res185}186190function markParentsDirty(proxy) {187function markDirty(proxy) {188 proxy[CHANGED_STATE] = true189 let parent = proxy190 while ((parent = parent[PARENT])) {193 if (parent[CHANGED_STATE]) return191 if (parent[CHANGED_STATE] === true) return192 parent[CHANGED_STATE] = true193 }194}195
65 unmodified lines