Skip to content
Myr

more code reuse

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 4219c49 · Browse files · 4 files+69 added−69 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+69 added−69 removed

  • other:+69 added−69 removed

4 files+69 added−69 removed

modified:src/common.js+59 added-19 removed
modified:src/es5.js+4 added-21 removed
1"use strict"2// @ts-check34import {5 autoFreeze,5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,10 finalizeNonProxiedObject,9 shallowCopy,10 verifyReturnValue,13 each11 each,12 finalize13} from "./common"1415const descriptors = {}16let states = null
125 unmodified lines
142function hasArrayChanges(state) {143 return state.proxy.length !== state.base.length144}145147function finalize(proxy) {148 // TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup149 if (isProxy(proxy)) {150 const state = proxy[PROXY_STATE]151 if (state.modified === true) {152 if (state.finalized === true) return state.copy153 state.finalized = true154 if (Array.isArray(state.base)) return finalizeArray(proxy, state)155 return finalizeObject(proxy, state)156 } else return state.base157 } else if (proxy !== null && typeof proxy === "object") {158 finalizeNonProxiedObject(proxy, finalize)159 }160 return proxy161}162163function finalizeObject(proxy, state) {146export function finalizeObject(proxy, state) {147 const res = (state.copy = shallowCopy(proxy))148 const base = state.base149 each(res, (prop, value) => {150 if (value !== base[prop]) res[prop] = finalize(value)151 })152 return freeze(res)153}154172function finalizeArray(proxy, state) {155export function finalizeArray(proxy, state) {156 const res = (state.copy = shallowCopy(proxy))157 const base = state.base158 each(res, (i, value) => {159 if (value !== base[i]) res[i] = finalize(value)
62 unmodified lines
modified:src/index.js+3 added-9 removed
modified:src/proxy.js+3 added-20 removed
1"use strict"2// @ts-check34import {5 autoFreeze,5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,10 finalizeNonProxiedObject,9 finalize,10 shallowCopy,11 verifyReturnValue,12 each13} from "./common"
110 unmodified lines
124 proxies.push(proxy)125 return proxy.proxy126}127129// given a base object, returns it if unmodified, or return the changed cloned if modified130function finalize(base) {131 if (isProxy(base)) {132 const state = base[PROXY_STATE]133 if (state.modified === true) {134 if (state.finalized === true) return state.copy135 state.finalized = true136 if (Array.isArray(state.base)) return finalizeArray(state)137 return finalizeObject(state)138 } else return state.base139 } else if (base !== null && typeof base === "object") {140 finalizeNonProxiedObject(base, finalize)141 }142 return base143}144145function finalizeObject(state) {128export function finalizeObject(state) {129 const copy = state.copy130 const base = state.base131 each(copy, (prop, value) => {132 if (value !== base[prop]) copy[prop] = finalize(value)133 })134 return freeze(copy)135}136154function finalizeArray(state) {137export function finalizeArray(state) {138 const copy = state.copy139 const base = state.base140 each(copy, (i, value) => {141 if (value !== base[i]) copy[i] = finalize(value)
21 unmodified lines