invalidate the proxy of the base state after immer function executes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #10
authored by Oussama Ben Brahim · committed by Oussama Ben Brahim · parent 101aac2 · Browse files · 2 files·+57 added−9 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+57 added−9 removed
- tests:+24 added−1 removed
- other:+33 added−8 removed
Provenance
- Commit
5475b84- Tree
d38b6c4- Author
Oussama Ben Brahim <benbraou@gmail.com>- Committer
Oussama Ben Brahim <benbraou@gmail.com>- Parent
101aac2
2 files changed+57 added−9 removed
modified:__tests__/base.js+24 added-1 removed
173 unmodified lines174 const nextState = immer(baseState, s => {175 s.aProp = undefined176 })177 expect(nextState).not.toBe(baseState)178- expect(baseState.aProp).toBe('hi')178+ expect(baseState.aProp).toBe("hi")179 expect(nextState.aProp).toBe(undefined)180 })181 182+ it("should revoke the proxy of the baseState after immer function is executed", () => {183+ let proxy184+ const nextState = immer(baseState, s => {185+ proxy = s186+ s.aProp = "hello"187+ })188+ expect(nextState).not.toBe(baseState)189+ expect(baseState.aProp).toBe("hi")190+ expect(nextState.aProp).toBe("hello")191+192+ expect(() => {193+ proxy.aProp = "Hallo"194+ }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/)195+ expect(() => {196+ const aProp = proxy.aProp197+ }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/) 198+199+ expect(nextState).not.toBe(baseState)200+ expect(baseState.aProp).toBe("hi")201+ expect(nextState.aProp).toBe("hello")202+203+ })204+ 205 afterEach(() => {206 expect(baseState).toBe(origBaseState)207 expect(baseState).toEqual(createBaseState())208 })48 unmodified linesmodified:immer.js+33 added-8 removed
123456789107 unmodified lines18192021161722232425262728293031323336 unmodified lines707172736374757677786869707179808182838485868788899079919293949532 unmodified lines1281291301311321331341351361371381391401411421431441451461471481491501511527 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) {36 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)32 unmodified lines // create proxy for root const rootClone = createProxy(baseState) // execute the thunk thunk(rootClone) // 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