Fixed bug where new objects where proxied
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 94ae587 · Browse files · 3 files·+22 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+22 added−24 removed
- other:+22 added−24 removed
Provenance
- Commit
4ad622f- Tree
6a6719d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
94ae587
3 files changed+22 added−24 removed
modified:src/common.js+10 added-19 removed
78 unmodified lines79808182838485838485868788899090919293949595969798991001019697989910010210310410510610710810910110210310416 unmodified lines78 unmodified lines : finalizeArrayEs5(base, state) return useProxies ? finalizeObjectProxy(state) : finalizeObjectEs5(base, state) } else return state.base } else if (base !== null && typeof base === "object") { finalizeNonProxiedObject(base) } else { return state.base } } finalizeNonProxiedObject(base) return base}
function finalizeNonProxiedObject(parent) {export function finalizeNonProxiedObject(parent) { // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well if (!isProxyable(parent)) return if (Object.isFrozen(parent)) return if (Array.isArray(parent)) { for (let i = 0; i < parent.length; i++) { const child = parent[i] if (isProxy(child)) { parent[i] = finalize(child) } else finalizeNonProxiedObject(child) } each(parent, (i, child) => { if (isProxy(child)) { parent[i] = finalize(child) } else finalizeNonProxiedObject(child) }) } else { for (let key in parent) { const child = parent[key] if (isProxy(child)) { parent[key] = finalize(child) } else finalizeNonProxiedObject(child) } } // always freeze completely new data freeze(parent)}
16 unmodified linesmodified:src/es5.js+6 added-2 removed
35 unmodified lines36 37 function get(state, prop) {38 assertUnfinished(state)39 const value = source(state)[prop]40- if (!state.finalizing && !isProxy(value) && isProxyable(value)) {40+ if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {41+ // only create a proxy if the value is proxyable, and the value was in the base state42+ // if it wasn't in the base state, the object is already modified and we will process it in finalize43 prepareCopy(state)44 return (state.copy[prop] = createProxy(state, value))45 }46 return value107 unmodified lines154 })155 return freeze(res)156 }157 158+// TODO: unduplicate159 export function finalizeArray(proxy, state) {160 const res = (state.copy = shallowCopy(proxy))161+ const base = state.base162 each(res, (i, value) => {159- res[i] = finalize(value)163+ if (value !== base[i]) res[i] = finalize(value)164 })165 return freeze(res)166 }167 58 unmodified linesmodified:src/proxy.js+6 added-3 removed
8 unmodified lines9 PROXY_STATE,10 finalize,11 shallowCopy,12 verifyReturnValue,13- each13+ each,14+ finalizeNonProxiedObject15 } from "./common"16 17 let proxies = null18 40 unmodified lines59 function get(state, prop) {60 if (prop === PROXY_STATE) return state61 if (state.modified) {62 const value = state.copy[prop]62- if (!isProxy(value) && isProxyable(value))63+ 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)66 return (state.copy[prop] = createProxy(state, value))67 return value68 } else {69 if (prop in state.proxies) return state.proxies[prop]70 unmodified lines140 141 export function finalizeArray(state) {142 const copy = state.copy143 each(copy, (i, value) => {141- copy[i] = finalize(value)144+ if (state.base[i] !== value) copy[i] = finalize(value)145 })146 return freeze(copy)147 }148 17 unmodified lines