Skip to content
Myr

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

2 files+53 added−40 removed

modified:__tests__/base.js+9 added
modified:immer.js+44 added-40 removed
1// @ts-check23const sourceSymbol = Symbol()43/**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 returned
3 unmodified lines
10 * @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 */13function immer(baseState, thunk) {14 // Maps baseState objects to proxies15 const proxies = new Map()16 // Maps baseState objects to their copies17 const copies = new Map()1819 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 lines
41 return true42 }43 }4445 // 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 }5455 // 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 }6060 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 }7165 function proxyObject(object) {66 if (proxies.has(object)) return proxies.get(object)6768 const proxy = new Proxy(object, objectTraps)69 proxies.set(object, proxy)70 return proxy71 }7273 function finalizeThing(thing) {74 if (isPlainObject(thing)) return finalizeObject(thing)75 if (Array.isArray(thing)) return finalizeArray(thing)76 return thing77 }7879 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 }8586 // 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 }9293 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 }101102 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 }110111 // 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}118119function isPlainObject(value) {120 if (value === null || typeof value !== "object") return false
5 unmodified lines