immer.js
This file
Path
immer.js
170 lines · 5.4 KiB · JavaScript
Last changed by
Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
History of this fileTake it
Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 1–6why, one step back1/**2 * @typedef {Object} RevocableProxy3 * @property {any} proxy4 * @property {Function} revoke5 */6More repo setupMichel Weststratebefore adoption1637393
lines 7–8why, one step back7// @ts-check8Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 9–10why, one step back9const isProxySymbol = Symbol("immer-proxy")10More repo setupMichel Weststratebefore adoption1637393
line 11why, one step back11/**Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 12why, one step back12 * Immer takes a state, and runs a function against it.More repo setupMichel Weststratebefore adoption1637393
lines 13–21why, one step back13 * That function can freely mutate the state, as it will create copies-on-write.14 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned15 *16 * @export17 * @param {any} baseState - the state to start with18 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified19 * @returns {any} a new state, or the base state if nothing was modified20 */21function immer(baseState, thunk) {Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 22–27why, one step back2223 /**24 * Maps baseState objects to revocable proxies25 * @type {Map<Object,RevocableProxy>}26 */27 const revocableProxies = new Map()Code cleanupMichel Weststratebefore adoption2b2d0cb
line 28why, one step back28 // Maps baseState objects to their copiesMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 29why, one step back29More repo setupMichel Weststratebefore adoption1637393
lines 30–33why, one step back30 const copies = new Map()3132 const objectTraps = {33 get(target, prop) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 34why, one step back34 if (prop === isProxySymbol) return targetCode cleanupMichel Weststratebefore adoption2b2d0cb
line 35why, one step back35 return createProxy(getCurrentSource(target)[prop])More repo setupMichel Weststratebefore adoption1637393
lines 36–43why, one step back36 },37 has(target, prop) {38 return prop in getCurrentSource(target)39 },40 ownKeys(target) {41 return Reflect.ownKeys(getCurrentSource(target))42 },43 set(target, prop, value) {Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 44–45why, one step back44 const current = createProxy(getCurrentSource(target)[prop])45 const newValue = createProxy(value)More repo setupMichel Weststratebefore adoption1637393
line 46why, one step back46 if (current !== newValue) {Add support for arraysMichel Weststratebefore adoptiond3696cb
line 47why, one step back47 const copy = getOrCreateCopy(target)Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 48–50why, one step back48 copy[prop] = isProxy(newValue)49 ? newValue[isProxySymbol]50 : newValueMore repo setupMichel Weststratebefore adoption1637393
lines 51–54why, one step back51 }52 return true53 },54 deleteProperty(target, property) {Add support for arraysMichel Weststratebefore adoptiond3696cb
line 55why, one step back55 const copy = getOrCreateCopy(target)More repo setupMichel Weststratebefore adoption1637393
lines 56–57why, one step back56 delete copy[property]57 return trueMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 58why, one step back58 },More repo setupMichel Weststratebefore adoption1637393
lines 59–60why, one step back59 }60Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 61–63why, one step back61 // creates a copy for a base object if there ain't one62 function getOrCreateCopy(base) {63 let copy = copies.get(base)More repo setupMichel Weststratebefore adoption1637393
line 64why, one step back64 if (!copy) {Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 65–66why, one step back65 copy = Array.isArray(base) ? base.slice() : Object.assign({}, base)66 copies.set(base, copy)More repo setupMichel Weststratebefore adoption1637393
lines 67–70why, one step back67 }68 return copy69 }70Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 71why, one step back71 // returns the current source of truth for a base objectCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 72–74why, one step back72 function getCurrentSource(base) {73 const copy = copies.get(base)74 return copy || baseMore repo setupMichel Weststratebefore adoption1637393
line 75why, one step back75 }Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 76why, one step back76 Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 77–79why, one step back77 // creates a proxy for plain objects / arrays78 function createProxy(base) {79 if (isPlainObject(base) || Array.isArray(base)) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 80why, one step back80 if (isProxy(base)) return base // avoid double wrappingMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 81–85why, one step back81 if (revocableProxies.has(base)) return revocableProxies.get(base).proxy82 //const proxy = new Proxy(base, objectTraps)83 const revocableProxy = Proxy.revocable(base, objectTraps)84 revocableProxies.set(base, revocableProxy)85 return revocableProxy.proxyCode cleanupMichel Weststratebefore adoption2b2d0cb
lines 86–87why, one step back86 }87 return baseMore repo setupMichel Weststratebefore adoption1637393
lines 88–89why, one step back88 }89Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 90–92why, one step back90 // checks if the given base object has modifications, either because it is modified, or91 // because one of it's children is92 function hasChanges(base) {Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 93why, one step back93 const proxy = revocableProxies.get(base)More repo setupMichel Weststratebefore adoption1637393
lines 94–98why, one step back94 if (!proxy) return false // nobody did read this object95 if (copies.has(base)) return true // a copy was created, so there are changes96 // look deeper97 const keys = Object.keys(base)98 for (let i = 0; i < keys.length; i++) {Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 99why, one step back99 const value = base[keys[i]]Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 100–104why, one step back100 if (101 (Array.isArray(value) || isPlainObject(value)) &&102 hasChanges(value)103 )104 return trueMore repo setupMichel Weststratebefore adoption1637393
lines 105–108why, one step back105 }106 return false107 }108Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 109–115why, one step back109 // given a base object, returns it if unmodified, or return the changed cloned if modified110 function finalize(base) {111 if (isPlainObject(base)) return finalizeObject(base)112 if (Array.isArray(base)) return finalizeArray(base)113 return base114 }115More repo setupMichel Weststratebefore adoption1637393
line 116why, one step back116 function finalizeObject(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 117why, one step back117 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 118why, one step back118 const copy = getOrCreateCopy(thing)More repo setupMichel Weststratebefore adoption1637393
line 119why, one step back119 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 120why, one step back120 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 121–125why, one step back121 })122 return copy123 }124125 function finalizeArray(thing) {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 126why, one step back126 if (!hasChanges(thing)) return thingAdd support for arraysMichel Weststratebefore adoptiond3696cb
lines 127–128why, one step back127 const copy = getOrCreateCopy(thing)128 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 129why, one step back129 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
lines 130–133why, one step back130 })131 return copy132 }133Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 134–136why, one step back134 // create proxy for root135 const rootClone = createProxy(baseState)136 // execute the thunkMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 137–142why, one step back137 const maybeVoidReturn = thunk(rootClone)138 //values either than undefined will trigger warning;139 !Object.is(maybeVoidReturn, undefined) &&140 console.warn(141 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`,142 )Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 143–144why, one step back143 // revoke all proxies144 revoke(revocableProxies)Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 145–146why, one step back145 // and finalize the modified proxy146 return finalize(baseState)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 147–157why, one step back147}148149/**150 * Revoke all the proxies stored in the revocableProxies map151 * 152 * @param {Map<Object,RevocableProxy>} revocableProxies153 */154function revoke (revocableProxies){155 for (var revocableProxy of revocableProxies.values()) {156 revocableProxy.revoke()157 }More repo setupMichel Weststratebefore adoption1637393
lines 158–163why, one step back158}159160function isPlainObject(value) {161 if (value === null || typeof value !== "object") return false162 const proto = Object.getPrototypeOf(value)163 return proto === Object.prototype || proto === nullFixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 164–166why, one step back164}165166function isProxy(value) {Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
line 167why, one step back167 return !!value && !!value[isProxySymbol]More repo setupMichel Weststratebefore adoption1637393
lines 168–170why, one step back168}169170module.exports = immer