immer.js
This file
Path
immer.js
134 lines · 4.4 KiB · JavaScript
Last changed by
Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
History of this fileTake it
More repo setupMichel Weststratebefore adoption1637393
lines 1–2why, one step back1// @ts-check2Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 3–4why, one step back3const isProxySymbol = Symbol("immer-proxy")4More repo setupMichel Weststratebefore adoption1637393
line 5why, one step back5/**Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 6why, one step back6 * Immer takes a state, and runs a function against it.More repo setupMichel Weststratebefore adoption1637393
lines 7–15why, one step back7 * 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) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 16why, one step back16 // Maps baseState objects to proxiesMore repo setupMichel Weststratebefore adoption1637393
line 17why, one step back17 const proxies = new Map()Code cleanupMichel Weststratebefore adoption2b2d0cb
line 18why, one step back18 // Maps baseState objects to their copiesMore repo setupMichel Weststratebefore adoption1637393
lines 19–22why, one step back19 const copies = new Map()2021 const objectTraps = {22 get(target, prop) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 23why, one step back23 if (prop === isProxySymbol) return targetCode cleanupMichel Weststratebefore adoption2b2d0cb
line 24why, one step back24 return createProxy(getCurrentSource(target)[prop])More repo setupMichel Weststratebefore adoption1637393
lines 25–32why, one step back25 },26 has(target, prop) {27 return prop in getCurrentSource(target)28 },29 ownKeys(target) {30 return Reflect.ownKeys(getCurrentSource(target))31 },32 set(target, prop, value) {Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 33–34why, one step back33 const current = createProxy(getCurrentSource(target)[prop])34 const newValue = createProxy(value)More repo setupMichel Weststratebefore adoption1637393
line 35why, one step back35 if (current !== newValue) {Add support for arraysMichel Weststratebefore adoptiond3696cb
line 36why, one step back36 const copy = getOrCreateCopy(target)Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 37why, one step back37 copy[prop] = isProxy(newValue) ? newValue[isProxySymbol] : newValueMore repo setupMichel Weststratebefore adoption1637393
lines 38–41why, one step back38 }39 return true40 },41 deleteProperty(target, property) {Add support for arraysMichel Weststratebefore adoptiond3696cb
line 42why, one step back42 const copy = getOrCreateCopy(target)More repo setupMichel Weststratebefore adoption1637393
lines 43–47why, one step back43 delete copy[property]44 return true45 }46 }47Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 48–50why, one step back48 // creates a copy for a base object if there ain't one49 function getOrCreateCopy(base) {50 let copy = copies.get(base)More repo setupMichel Weststratebefore adoption1637393
line 51why, one step back51 if (!copy) {Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 52–53why, one step back52 copy = Array.isArray(base) ? base.slice() : Object.assign({}, base)53 copies.set(base, copy)More repo setupMichel Weststratebefore adoption1637393
lines 54–57why, one step back54 }55 return copy56 }57Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 58why, one step back58 // returns the current source of truth for a base objectCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 59–61why, one step back59 function getCurrentSource(base) {60 const copy = copies.get(base)61 return copy || baseMore repo setupMichel Weststratebefore adoption1637393
lines 62–63why, one step back62 }63Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 64–66why, one step back64 // creates a proxy for plain objects / arrays65 function createProxy(base) {66 if (isPlainObject(base) || Array.isArray(base)) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 67why, one step back67 if (isProxy(base)) return base // avoid double wrappingCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 68–73why, one step back68 if (proxies.has(base)) return proxies.get(base)69 const proxy = new Proxy(base, objectTraps)70 proxies.set(base, proxy)71 return proxy72 }73 return baseMore repo setupMichel Weststratebefore adoption1637393
lines 74–75why, one step back74 }75Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 76–79why, one step back76 // checks if the given base object has modifications, either because it is modified, or77 // because one of it's children is78 function hasChanges(base) {79 const proxy = proxies.get(base)More repo setupMichel Weststratebefore adoption1637393
lines 80–84why, one step back80 if (!proxy) return false // nobody did read this object81 if (copies.has(base)) return true // a copy was created, so there are changes82 // look deeper83 const keys = Object.keys(base)84 for (let i = 0; i < keys.length; i++) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 85–86why, one step back85 const value = base[keys[i]]86 if ((Array.isArray(value) || isPlainObject(value)) && hasChanges(value)) return trueMore repo setupMichel Weststratebefore adoption1637393
lines 87–90why, one step back87 }88 return false89 }90Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 91–97why, one step back91 // given a base object, returns it if unmodified, or return the changed cloned if modified92 function finalize(base) {93 if (isPlainObject(base)) return finalizeObject(base)94 if (Array.isArray(base)) return finalizeArray(base)95 return base96 }97More repo setupMichel Weststratebefore adoption1637393
line 98why, one step back98 function finalizeObject(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 99why, one step back99 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 100why, one step back100 const copy = getOrCreateCopy(thing)More repo setupMichel Weststratebefore adoption1637393
line 101why, one step back101 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 102why, one step back102 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 103–107why, one step back103 })104 return copy105 }106107 function finalizeArray(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 108why, one step back108 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
lines 109–110why, one step back109 const copy = getOrCreateCopy(thing)110 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 111why, one step back111 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
lines 112–115why, one step back112 })113 return copy114 }115Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 116–118why, one step back116 // create proxy for root117 const rootClone = createProxy(baseState)118 // execute the thunkMore repo setupMichel Weststratebefore adoption1637393
line 119why, one step back119 thunk(rootClone)Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 120–121why, one step back120 // and finalize the modified proxy121 return finalize(baseState)More repo setupMichel Weststratebefore adoption1637393
lines 122–127why, one step back122}123124function isPlainObject(value) {125 if (value === null || typeof value !== "object") return false126 const proto = Object.getPrototypeOf(value)127 return proto === Object.prototype || proto === nullFixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 128–130why, one step back128}129130function isProxy(value) {Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
line 131why, one step back131 return !!value && !!value[isProxySymbol]More repo setupMichel Weststratebefore adoption1637393
lines 132–134why, one step back132}133134module.exports = immer