immer.js
This file
Path
immer.js
147 lines · 4.8 KiB · JavaScript
Last changed by
Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
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)Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 37–39why, one step back37 copy[prop] = isProxy(newValue)38 ? newValue[isProxySymbol]39 : newValueMore repo setupMichel Weststratebefore adoption1637393
lines 40–43why, one step back40 }41 return true42 },43 deleteProperty(target, property) {Add support for arraysMichel Weststratebefore adoptiond3696cb
line 44why, one step back44 const copy = getOrCreateCopy(target)More repo setupMichel Weststratebefore adoption1637393
lines 45–46why, one step back45 delete copy[property]46 return trueMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 47why, one step back47 },More repo setupMichel Weststratebefore adoption1637393
lines 48–49why, one step back48 }49Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 50–52why, one step back50 // creates a copy for a base object if there ain't one51 function getOrCreateCopy(base) {52 let copy = copies.get(base)More repo setupMichel Weststratebefore adoption1637393
line 53why, one step back53 if (!copy) {Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 54–55why, one step back54 copy = Array.isArray(base) ? base.slice() : Object.assign({}, base)55 copies.set(base, copy)More repo setupMichel Weststratebefore adoption1637393
lines 56–59why, one step back56 }57 return copy58 }59Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 60why, one step back60 // returns the current source of truth for a base objectCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 61–63why, one step back61 function getCurrentSource(base) {62 const copy = copies.get(base)63 return copy || baseMore repo setupMichel Weststratebefore adoption1637393
lines 64–65why, one step back64 }65Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 66–68why, one step back66 // creates a proxy for plain objects / arrays67 function createProxy(base) {68 if (isPlainObject(base) || Array.isArray(base)) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 69why, one step back69 if (isProxy(base)) return base // avoid double wrappingCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 70–75why, one step back70 if (proxies.has(base)) return proxies.get(base)71 const proxy = new Proxy(base, objectTraps)72 proxies.set(base, proxy)73 return proxy74 }75 return baseMore repo setupMichel Weststratebefore adoption1637393
lines 76–77why, one step back76 }77Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 78–81why, one step back78 // checks if the given base object has modifications, either because it is modified, or79 // because one of it's children is80 function hasChanges(base) {81 const proxy = proxies.get(base)More repo setupMichel Weststratebefore adoption1637393
lines 82–86why, one step back82 if (!proxy) return false // nobody did read this object83 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++) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 87why, one step back87 const value = base[keys[i]]Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 88–92why, one step back88 if (89 (Array.isArray(value) || isPlainObject(value)) &&90 hasChanges(value)91 )92 return trueMore repo setupMichel Weststratebefore adoption1637393
lines 93–96why, one step back93 }94 return false95 }96Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 97–103why, one step back97 // given a base object, returns it if unmodified, or return the changed cloned if modified98 function finalize(base) {99 if (isPlainObject(base)) return finalizeObject(base)100 if (Array.isArray(base)) return finalizeArray(base)101 return base102 }103More repo setupMichel Weststratebefore adoption1637393
line 104why, one step back104 function finalizeObject(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 105why, one step back105 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 106why, one step back106 const copy = getOrCreateCopy(thing)More repo setupMichel Weststratebefore adoption1637393
line 107why, one step back107 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 108why, one step back108 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 109–113why, one step back109 })110 return copy111 }112113 function finalizeArray(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 114why, one step back114 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
lines 115–116why, one step back115 const copy = getOrCreateCopy(thing)116 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 117why, one step back117 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
lines 118–121why, one step back118 })119 return copy120 }121Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 122–124why, one step back122 // create proxy for root123 const rootClone = createProxy(baseState)124 // execute the thunkMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 125–132why, one step back125 const maybeVoidReturn = thunk(rootClone)126127 //values either than undefined will trigger warning;128 !Object.is(maybeVoidReturn, undefined) &&129 console.warn(130 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`,131 )132Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 133–134why, one step back133 // and finalize the modified proxy134 return finalize(baseState)More repo setupMichel Weststratebefore adoption1637393
lines 135–140why, one step back135}136137function isPlainObject(value) {138 if (value === null || typeof value !== "object") return false139 const proto = Object.getPrototypeOf(value)140 return proto === Object.prototype || proto === nullFixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 141–143why, one step back141}142143function isProxy(value) {Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
line 144why, one step back144 return !!value && !!value[isProxySymbol]More repo setupMichel Weststratebefore adoption1637393
lines 145–147why, one step back145}146147module.exports = immer