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
Provenance
- Commit
93372fc- Tree
8756d10- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
4219c49
4 files changed+69 added−69 removed
modified:src/common.js+59 added-19 removed
12123456789101112131415161718192021222324252627282930313233343536373811 unmodified lines50515253232425262728293031323334545556574 unmodified lines626364654766676869707172737475767778798081828384858687888990919293949556579697989910010110263641031041051061071089 unmodified linesexport const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer versionexport let autoFreeze = trueimport { finalizeArray as finalizeArrayProxy, finalizeObject as finalizeObjectProxy} from "./proxy"import { finalizeArray as finalizeArrayEs5, finalizeObject as finalizeObjectEs5} from "./es5"
export const PROXY_STATE = Symbol("immer-proxy-state")
let autoFreeze = truelet useProxies = typeof Proxy !== "undefined"
/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production. * It is by default enabled. * * @returns {void} */export function setAutoFreeze(enableAutoFreeze) { autoFreeze = enableAutoFreeze}
export function setUseProxies(value) { useProxies = value}
export function getUseProxies() { return useProxies}
export function isProxy(value) { return !!value && !!value[PROXY_STATE]}
11 unmodified lines } return value}
/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production. * It is by default enabled. * * @returns {void} */export function setAutoFreeze(enableAutoFreeze) { autoFreeze = enableAutoFreeze}
export function shallowCopy(value) { return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?}
4 unmodified lines for (let key in value) cb(key, value[key]) }}
export function finalizeNonProxiedObject(parent, finalizer) {// given a base object, returns it if unmodified, or return the changed cloned if modifiedexport function finalize(base) { if (isProxy(base)) { const state = base[PROXY_STATE] if (state.modified === true) { if (state.finalized === true) return state.copy state.finalized = true if (Array.isArray(state.base)) return useProxies ? finalizeArrayProxy(state) : finalizeArrayEs5(base, state) return useProxies ? finalizeObjectProxy(state) : finalizeObjectEs5(base, state) } else return state.base } else if (base !== null && typeof base === "object") { finalizeNonProxiedObject(base) } return base}
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] = finalizer(child) } else finalizeNonProxiedObject(child, finalizer) parent[i] = finalize(child) } else finalizeNonProxiedObject(child) } } else { for (let key in parent) { const child = parent[key] if (isProxy(child)) { parent[key] = finalizer(child) } else finalizeNonProxiedObject(child, finalizer) parent[key] = finalize(child) } else finalizeNonProxiedObject(child) } } // always freeze completely new data freeze(parent)9 unmodified linesmodified:src/es5.js+4 added-21 removed
1 "use strict"2 // @ts-check3 4 import {5- autoFreeze,5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,10- finalizeNonProxiedObject,9 shallowCopy,10 verifyReturnValue,13- each11+ each,12+ finalize13 } from "./common"14 15 const descriptors = {}16 let states = null125 unmodified lines142 function hasArrayChanges(state) {143 return state.proxy.length !== state.base.length144 }145 147-function 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-}162-163-function finalizeObject(proxy, state) {146+export 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 }154 172-function finalizeArray(proxy, state) {155+export 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 linesmodified:src/index.js+3 added-9 removed
11233456787891024 unmodified lines35363738413940414245464748export {setAutoFreeze} from "./common"export {setAutoFreeze, setUseProxies} from "./common"
import {isProxyable} from "./common"import {isProxyable, getUseProxies} from "./common"import {produceProxy} from "./proxy"import {produceEs5} from "./es5"
let useProxies = typeof Proxy !== "undefined"
/** * produce takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned24 unmodified lines if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState)) if (typeof producer !== "function") throw new Error("the second argument to produce should be a function") }
return useProxies return getUseProxies() ? produceProxy(baseState, producer) : produceEs5(baseState, producer)}
export function setUseProxies(value) { useProxies = value}modified:src/proxy.js+3 added-20 removed
1 "use strict"2 // @ts-check3 4 import {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 lines124 proxies.push(proxy)125 return proxy.proxy126 }127 129-// given a base object, returns it if unmodified, or return the changed cloned if modified130-function 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-}144-145-function finalizeObject(state) {128+export 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 }136 154-function finalizeArray(state) {137+export 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