Fixed es5 implementation
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 c420f0d · Browse files · 1 file·+36 added−13 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+36 added−13 removed
- other:+36 added−13 removed
Provenance
- Commit
931e18a- Tree
3d4f200- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
c420f0d
1 file changed+36 added−13 removed
modified:src/es5.js+36 added-13 removed
167 unmodified lines168 return state.proxy.length !== state.base.length169 }170 171 function finalize(proxy) {172- // given a base object, returns it if unmodified, or return the changed cloned if modified173- if (!isProxy(proxy)) return proxy174- const state = proxy[PROXY_STATE]175- if (state.modified === false) return state.base // return the original target176- if (Array.isArray(proxy)) return finalizeArray(proxy)177- return finalizeObject(proxy)172+ // TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup173+ if (isProxy(proxy)) {174+ const state = proxy[PROXY_STATE]175+ if (state.modified === true) {176+ if (Array.isArray(state.base))177+ return finalizeArray(proxy, state)178+ return finalizeObject(proxy, state)179+ } else return state.base180+ } else if (proxy !== null && typeof proxy === "object") {181+ // 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 original182+ // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well183+ if (Array.isArray(proxy)) {184+ for (let i = 0; i < proxy.length; i++)185+ proxy[i] = finalize(proxy[i])186+ return freeze(proxy)187+ }188+ const proto = Object.getPrototypeOf(proxy)189+ if (proto === null || proto === Object.prototype) {190+ for (let key in proxy) proxy[key] = finalize(proxy[key])191+ return freeze(proxy)192+ }193+ }194+ return proxy195 }196 180- function finalizeObject(proxy) {197+ function finalizeObject(proxy, state) {198 const res = Object.assign({}, proxy)182- Object.keys(res).forEach(prop => {183- res[prop] = finalize(res[prop])184- })199+ const base = state.base200+ for (let prop in res) {201+ if (proxy[prop] !== base[prop]) res[prop] = finalize(res[prop])202+ }203 return freeze(res)204 }205 188- function finalizeArray(proxy) {189- return freeze(proxy.map(finalize))206+ function finalizeArray(proxy, state) {207+ const res = proxy.slice()208+ const base = state.base209+ for (let i = 0; i < res.length; i++) {210+ if (res[i] !== base[i]) res[i] = finalize(res[i])211+ }212+ return freeze(res)213 }214 215 // create proxy for root216 const rootClone = createProxy(undefined, baseState)22 unmodified lines239 if (!value) return false240 if (typeof value !== "object") return false241 if (Array.isArray(value)) return true242 const proto = Object.getPrototypeOf(value)220- return (proto === proto) === null || Object.prototype243+ return proto === null || proto === Object.prototype244 }245 246 function freeze(value) {247 if (autoFreeze) {49 unmodified lines