Merge pull request #14 from benbraou/issue10
Before adoption: this commit came in with the history the project adopted, so no landing records it.
invalidate the proxy of the base state after immer function executes
authored by Michel Weststrate · committed by GitHub · parents a151fed, 6a2544d · Browse files · 2 files·+56 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−10 removed
- tests:+23 added−0 removed
- other:+33 added−10 removed
Provenance
- Commit
e775cca- Tree
17dc315- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a151fed- Parent 2
6a2544d
2 files changed+56 added−10 removed
modified:__tests__/base.js+23 added
187 unmodified lines188 expect(baseState.aProp).toBe("hi")189 expect(nextState.aProp).toBe(undefined)190 })191 192+ it("should revoke the proxy of the baseState after immer function is executed", () => {193+ let proxy194+ const nextState = immer(baseState, s => {195+ proxy = s196+ s.aProp = "hello"197+ })198+ expect(nextState).not.toBe(baseState)199+ expect(baseState.aProp).toBe("hi")200+ expect(nextState.aProp).toBe("hello")201+202+ expect(() => {203+ proxy.aProp = "Hallo"204+ }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/)205+ expect(() => {206+ const aProp = proxy.aProp207+ }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/) 208+209+ expect(nextState).not.toBe(baseState)210+ expect(baseState.aProp).toBe("hi")211+ expect(nextState.aProp).toBe("hello")212+213+ })214+ 215 afterEach(() => {216 expect(baseState).toBe(origBaseState)217 expect(baseState).toEqual(createBaseState())218 })48 unmodified linesmodified:immer.js+33 added-10 removed
123456789107 unmodified lines18192021161722232425262728293031323338 unmodified lines727374756576777879807071727381828384858687888990919281939495969736 unmodified lines1341351361371261381391401411421321431441451461471481491501511521531541551561571581591601611621637 unmodified lines/** * @typedef {Object} RevocableProxy * @property {any} proxy * @property {Function} revoke */
// @ts-check
const isProxySymbol = Symbol("immer-proxy")
7 unmodified lines * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified * @returns {any} a new state, or the base state if nothing was modified */function immer(baseState, thunk) { // Maps baseState objects to proxies const proxies = new Map()
/** * Maps baseState objects to revocable proxies * @type {Map<Object,RevocableProxy>} */ const revocableProxies = new Map() // Maps baseState objects to their copies
const copies = new Map()
const objectTraps = { get(target, prop) {38 unmodified lines function getCurrentSource(base) { const copy = copies.get(base) return copy || base }
// creates a proxy for plain objects / arrays function createProxy(base) { if (isPlainObject(base) || Array.isArray(base)) { if (isProxy(base)) return base // avoid double wrapping if (proxies.has(base)) return proxies.get(base) const proxy = new Proxy(base, objectTraps) proxies.set(base, proxy) return proxy if (revocableProxies.has(base)) return revocableProxies.get(base).proxy //const proxy = new Proxy(base, objectTraps) const revocableProxy = Proxy.revocable(base, objectTraps) revocableProxies.set(base, revocableProxy) return revocableProxy.proxy } return base }
// checks if the given base object has modifications, either because it is modified, or // because one of it's children is function hasChanges(base) { const proxy = proxies.get(base) const proxy = revocableProxies.get(base) if (!proxy) return false // nobody did read this object if (copies.has(base)) return true // a copy was created, so there are changes // look deeper const keys = Object.keys(base)36 unmodified lines // create proxy for root const rootClone = createProxy(baseState) // execute the thunk const maybeVoidReturn = thunk(rootClone)
//values either than undefined will trigger warning; !Object.is(maybeVoidReturn, undefined) && console.warn( `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`, )
// revoke all proxies revoke(revocableProxies) // and finalize the modified proxy return finalize(baseState)}
/** * Revoke all the proxies stored in the revocableProxies map * * @param {Map<Object,RevocableProxy>} revocableProxies */function revoke (revocableProxies){ for (var revocableProxy of revocableProxies.values()) { revocableProxy.revoke() }}
function isPlainObject(value) { if (value === null || typeof value !== "object") return false const proto = Object.getPrototypeOf(value) return proto === Object.prototype || proto === null7 unmodified lines