Skip to content
Myr

immer.js

This file

Path

immer.js

121 lines · 3.7 KiB · JavaScript

Last changed by

Add support for arraysMichel Weststratebefore adoptiond3696cb

History of this file

Take it

More repo setupMichel Weststratebefore adoption1637393

lines 1–30why, one step back
1// @ts-check
23const sourceSymbol = Symbol()45/**6 * Immer takes a state, and runs a function agains it.7 * That function can freely mutate the state, as it will create copies-on-write.8 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned9 *10 * @export11 * @param {any} baseState - the state to start with12 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified13 * @returns {any} a new state, or the base state if nothing was modified14 */15function immer(baseState, thunk) {16 const proxies = new Map()17 const copies = new Map()1819 const objectTraps = {20 get(target, prop) {21 if (prop === sourceSymbol) return target22 return proxyThing(getCurrentSource(target)[prop])23 },24 has(target, prop) {25 return prop in getCurrentSource(target)26 },27 ownKeys(target) {28 return Reflect.ownKeys(getCurrentSource(target))29 },30 set(target, prop, value) {

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 31why, one step back
31 const current = proxyThing(getCurrentSource(target)[prop])

More repo setupMichel Weststratebefore adoption1637393

lines 32–33why, one step back
32 const newValue = proxyThing(value)
33 if (current !== newValue) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 34why, one step back
34 const copy = getOrCreateCopy(target)

More repo setupMichel Weststratebefore adoption1637393

lines 35–39why, one step back
35 copy[prop] = newValue
36 }37 return true38 },39 deleteProperty(target, property) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 40why, one step back
40 const copy = getOrCreateCopy(target)

More repo setupMichel Weststratebefore adoption1637393

lines 41–45why, one step back
41 delete copy[property]
42 return true43 }44 }45

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 46why, one step back
46 function getOrCreateCopy(object) {

More repo setupMichel Weststratebefore adoption1637393

lines 47–48why, one step back
47 let copy = copies.get(object)
48 if (!copy) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 49why, one step back
49 copy = Array.isArray(object) ? object.slice() : Object.assign({}, object)

More repo setupMichel Weststratebefore adoption1637393

lines 50–60why, one step back
50 copies.set(object, copy)
51 }52 return copy53 }5455 function getCurrentSource(object) {56 const copy = copies.get(object)57 return copy || object58 }5960 function proxyThing(thing) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 61why, one step back
61 if (isPlainObject(thing) || Array.isArray(thing)) return proxyObject(thing)

More repo setupMichel Weststratebefore adoption1637393

lines 62–74why, one step back
62 return thing
63 }6465 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)

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 75why, one step back
75 if (Array.isArray(thing)) return finalizeArray(thing)

More repo setupMichel Weststratebefore adoption1637393

lines 76–93why, one step back
76 return thing
77 }7879 function hasObjectChanges(thing) {80 const proxy = proxies.get(thing)81 if (!proxy) return false // nobody did read this object82 const base = proxy[sourceSymbol]83 if (copies.has(base)) return true // a copy was created, so there are changes84 // look deeper85 const keys = Object.keys(base)86 for (let i = 0; i < keys.length; i++) {87 if (hasObjectChanges(base[keys[i]])) return true88 }89 return false90 }9192 function finalizeObject(thing) {93 if (!hasObjectChanges(thing)) return thing

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 94why, one step back
94 const copy = getOrCreateCopy(thing)

More repo setupMichel Weststratebefore adoption1637393

lines 95–96why, one step back
95 Object.keys(copy).forEach(prop => {
96 copy[prop] = finalizeThing(copy[prop])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 97–105why, one step back
97 })
98 return copy99 }100101 function finalizeArray(thing) {102 if (!hasObjectChanges(thing)) return thing103 const copy = getOrCreateCopy(thing)104 copy.forEach((value, index) => {105 copy[index] = finalizeThing(copy[index])

More repo setupMichel Weststratebefore adoption1637393

lines 106–121why, one step back
106 })
107 return copy108 }109110 const rootClone = proxyThing(baseState)111 thunk(rootClone)112 return finalizeThing(baseState)113}114115function isPlainObject(value) {116 if (value === null || typeof value !== "object") return false117 const proto = Object.getPrototypeOf(value)118 return proto === Object.prototype || proto === null119}120121module.exports = immer