Code cleanup
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 d3696cb · Browse files · 2 files·+53 added−40 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+53 added−40 removed
- tests:+9 added−0 removed
- other:+44 added−40 removed
Provenance
- Commit
2b2d0cb- Tree
028a0e6- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d3696cb
2 files changed+53 added−40 removed
modified:__tests__/base.js+9 added
114 unmodified linesExpand all11511611711811912012112212312412512612712812913013114 unmodified lines114 unmodified linesExpand all expect(nextState.anArray).not.toBe(baseState.anArray) expect(nextState.anArray).toEqual([1, 2, 3, { c: 5 }]) })
it("should updating inside arrays", () => { const nextState = immer(baseState, s => { s.anArray[2].test = true }) expect(nextState).not.toBe(baseState) expect(nextState.anArray).not.toBe(baseState.anArray) expect(nextState.anArray).toEqual([3, 2, { c: 3, test: true }, 1]) })
afterEach(() => { expect(baseState).toBe(origBaseState) expect(baseState).toEqual(createBaseState()) })14 unmodified linesmodified:immer.js+44 added-40 removed
1 // @ts-check2 3-const sourceSymbol = Symbol()4-3 /**4 * Immer takes a state, and runs a function agains it.5 * That function can freely mutate the state, as it will create copies-on-write.6 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned3 unmodified lines10 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified11 * @returns {any} a new state, or the base state if nothing was modified12 */13 function immer(baseState, thunk) {14+ // Maps baseState objects to proxies15 const proxies = new Map()16+ // Maps baseState objects to their copies17 const copies = new Map()18 19 const objectTraps = {20 get(target, prop) {21- if (prop === sourceSymbol) return target22- return proxyThing(getCurrentSource(target)[prop])21+ return createProxy(getCurrentSource(target)[prop])22 },23 has(target, prop) {24 return prop in getCurrentSource(target)25 },26 ownKeys(target) {27 return Reflect.ownKeys(getCurrentSource(target))28 },29 set(target, prop, value) {31- const current = proxyThing(getCurrentSource(target)[prop])32- const newValue = proxyThing(value)30+ const current = createProxy(getCurrentSource(target)[prop])31+ const newValue = createProxy(value)32 if (current !== newValue) {33 const copy = getOrCreateCopy(target)34 copy[prop] = newValue35 }5 unmodified lines41 return true42 }43 }44 45+ // creates a copy for a base object if there ain't one46- function getOrCreateCopy(object) {47- let copy = copies.get(object)46+ function getOrCreateCopy(base) {47+ let copy = copies.get(base)48 if (!copy) {49- copy = Array.isArray(object) ? object.slice() : Object.assign({}, object)50- copies.set(object, copy)49+ copy = Array.isArray(base) ? base.slice() : Object.assign({}, base)50+ copies.set(base, copy)51 }52 return copy53 }54 55+ // returns the current source of trugth for a base object55- function getCurrentSource(object) {56- const copy = copies.get(object)57- return copy || object56+ function getCurrentSource(base) {57+ const copy = copies.get(base)58+ return copy || base59 }60 60- function proxyThing(thing) {61- if (isPlainObject(thing) || Array.isArray(thing)) return proxyObject(thing)62- return thing61+ // creates a proxy for plain objects / arrays62+ function createProxy(base) {63+ if (isPlainObject(base) || Array.isArray(base)) {64+ if (proxies.has(base)) return proxies.get(base)65+ const proxy = new Proxy(base, objectTraps)66+ proxies.set(base, proxy)67+ return proxy68+ }69+ return base70 }71 65- function proxyObject(object) {66- if (proxies.has(object)) return proxies.get(object)67-68- const proxy = new Proxy(object, objectTraps)69- proxies.set(object, proxy)70- return proxy71- }72-73- function finalizeThing(thing) {74- if (isPlainObject(thing)) return finalizeObject(thing)75- if (Array.isArray(thing)) return finalizeArray(thing)76- return thing77- }78-79- function hasObjectChanges(thing) {80- const proxy = proxies.get(thing)72+ // checks if the given base object has modifications, either because it is modified, or73+ // because one of it's children is74+ function hasChanges(base) {75+ const proxy = proxies.get(base)76 if (!proxy) return false // nobody did read this object82- const base = proxy[sourceSymbol]77 if (copies.has(base)) return true // a copy was created, so there are changes78 // look deeper79 const keys = Object.keys(base)80 for (let i = 0; i < keys.length; i++) {87- if (hasObjectChanges(base[keys[i]])) return true81+ if (hasChanges(base[keys[i]])) return true82 }83 return false84 }85 86+ // given a base object, returns it if unmodified, or return the changed cloned if modified87+ function finalize(base) {88+ if (isPlainObject(base)) return finalizeObject(base)89+ if (Array.isArray(base)) return finalizeArray(base)90+ return base91+ }92+93 function finalizeObject(thing) {93- if (!hasObjectChanges(thing)) return thing94+ if (!hasChanges(thing)) return thing95 const copy = getOrCreateCopy(thing)96 Object.keys(copy).forEach(prop => {96- copy[prop] = finalizeThing(copy[prop])97+ copy[prop] = finalize(copy[prop])98 })99 return copy100 }101 102 function finalizeArray(thing) {102- if (!hasObjectChanges(thing)) return thing103+ if (!hasChanges(thing)) return thing104 const copy = getOrCreateCopy(thing)105 copy.forEach((value, index) => {105- copy[index] = finalizeThing(copy[index])106+ copy[index] = finalize(copy[index])107 })108 return copy109 }110 111+ // create proxy for root110- const rootClone = proxyThing(baseState)112+ const rootClone = createProxy(baseState)113+ // execute the thunk114 thunk(rootClone)115+ // and finalize the modified proxy112- return finalizeThing(baseState)116+ return finalize(baseState)117 }118 119 function isPlainObject(value) {120 if (value === null || typeof value !== "object") return false5 unmodified lines