immer.js
This file
Path
immer.js
256 lines · 7.8 KiB · JavaScript
Last changed by
`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
History of this fileTake it
Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 1–3why, one step back1"use strict"2// @ts-check3Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 4–8why, one step back4if (typeof Proxy === "undefined")5 throw new Error(6 "Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."7 )8Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 9–10why, one step back9const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version10let autoFreeze = trueMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 11why, one step back11Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 12–41why, one step back12const objectTraps = {13 get(target, prop) {14 if (prop === PROXY_STATE) return target15 return target.get(prop)16 },17 has(target, prop) {18 return prop in target.source19 },20 ownKeys(target) {21 return Reflect.ownKeys(target.source)22 },23 set(target, prop, value) {24 target.set(prop, value)25 return true26 },27 deleteProperty(target, prop) {28 target.deleteProp(prop)29 return true30 },31 getOwnPropertyDescriptor(target, prop) {32 return target.getOwnPropertyDescriptor(prop)33 },34 defineProperty(target, property, descriptor) {35 target.defineProperty(property, descriptor)36 return true37 },38 setPrototypeOf() {39 throw new Error("Don't even try this...")40 }41}More repo setupMichel Weststratebefore adoption1637393
line 42why, one step back42Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 43–72why, one step back43const arrayTraps = {44 get(target, prop) {45 if (prop === PROXY_STATE) return target[0]46 return target[0].get(prop)47 },48 has(target, prop) {49 return prop in target[0].source50 },51 ownKeys(target) {52 return Reflect.ownKeys(target[0].source)53 },54 set(target, prop, value) {55 target[0].set(prop, value)56 return true57 },58 deleteProperty(target, prop) {59 target[0].deleteProp(prop)60 return true61 },62 getOwnPropertyDescriptor(target, prop) {63 return target[0].getOwnPropertyDescriptor(prop)64 },65 defineProperty(target, property, descriptor) {66 target[0].defineProperty(property, descriptor)67 return true68 },69 setPrototypeOf() {70 throw new Error("Don't even try this...")71 }72}Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 73why, one step back73More repo setupMichel Weststratebefore adoption1637393
line 74why, one step back74/**Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 75why, one step back75 * Immer takes a state, and runs a function against it.More repo setupMichel Weststratebefore adoption1637393
lines 76–83why, one step back76 * That function can freely mutate the state, as it will create copies-on-write.77 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned78 *79 * @export80 * @param {any} baseState - the state to start with81 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified82 * @returns {any} a new state, or the base state if nothing was modified83 */`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 84why, one step back84function produce(baseState, thunk) {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 85why, one step back85 const revocableProxies = []Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 86why, one step back86Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 87–94why, one step back87 class State {88 constructor(parent, base) {89 this.modified = false90 this.parent = parent91 this.base = base92 this.copy = undefined93 this.proxies = {}94 }More repo setupMichel Weststratebefore adoption1637393
line 95why, one step back95Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 96–111why, one step back96 get source() {97 return this.modified === true ? this.copy : this.base98 }99100 get(prop) {101 if (this.modified) {102 const value = this.copy[prop]103 if (!isProxy(value) && isProxyable(value))104 return (this.copy[prop] = createProxy(this, value))105 return value106 } else {107 if (prop in this.proxies) return this.proxies[prop]108 const value = this.base[prop]109 if (!isProxy(value) && isProxyable(value))110 return (this.proxies[prop] = createProxy(this, value))111 return valueMore repo setupMichel Weststratebefore adoption1637393
line 112why, one step back112 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 113why, one step back113 }More repo setupMichel Weststratebefore adoption1637393
line 114why, one step back114Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 115–124why, one step back115 set(prop, value) {116 if (!this.modified) {117 if (118 (prop in this.base && this.base[prop] === value) ||119 (prop in this.proxies && this.proxies[prop] === value)120 )121 return122 this.markChanged()123 }124 this.copy[prop] = valueMore repo setupMichel Weststratebefore adoption1637393
lines 125–126why, one step back125 }126Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 127–130why, one step back127 deleteProp(prop) {128 this.markChanged()129 delete this.copy[prop]130 }Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 131why, one step back131Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 132–153why, one step back132 getOwnPropertyDescriptor(prop) {133 const owner = this.modified134 ? this.copy135 : prop in this.proxies ? this.proxies : this.base136 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)137 if (descriptor) descriptor.configurable = true // XXX: is this really needed?138 return descriptor139 }140141 defineProperty(property, descriptor) {142 this.markChanged()143 Object.defineProperty(this.copy, property, descriptor)144 }145146 markChanged() {147 if (!this.modified) {148 this.modified = true149 this.copy = Array.isArray(this.base) // TODO: eliminate those isArray checks?150 ? this.base.slice()151 : Object.assign({}, this.base)152 Object.assign(this.copy, this.proxies) // yup that works for arrays as well153 if (this.parent) this.parent.markChanged()Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 154why, one step back154 }Code cleanupMichel Weststratebefore adoption2b2d0cb
line 155why, one step back155 }More repo setupMichel Weststratebefore adoption1637393
lines 156–157why, one step back156 }157Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 158–167why, one step back158 // creates a proxy for plain objects / arrays159 function createProxy(parentState, base) {160 const state = new State(parentState, base)161 let proxy162 if (Array.isArray(base)) {163 // Proxy should be created with an array to make it an array for JS164 // so... here you have it!165 proxy = Proxy.revocable([state], arrayTraps)166 } else {167 proxy = Proxy.revocable(state, objectTraps)More repo setupMichel Weststratebefore adoption1637393
line 168why, one step back168 }Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 169–170why, one step back169 revocableProxies.push(proxy)170 return proxy.proxyMore repo setupMichel Weststratebefore adoption1637393
lines 171–172why, one step back171 }172Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 173–174why, one step back173 // given a base object, returns it if unmodified, or return the changed cloned if modified174 function finalize(base) {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 175–181why, one step back175 if (isProxy(base)) {176 const state = base[PROXY_STATE]177 if (state.modified === true) {178 if (Array.isArray(state.base)) return finalizeArray(state)179 return finalizeObject(state)180 } else return state.base181 }Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 182–184why, one step back182 return base183 }184Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 185–186why, one step back185 function finalizeObject(state) {186 const copy = state.copyMore repo setupMichel Weststratebefore adoption1637393
line 187why, one step back187 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 188why, one step back188 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
line 189why, one step back189 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 190why, one step back190 return freeze(copy)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 191–192why, one step back191 }192Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 193–194why, one step back193 function finalizeArray(state) {194 const copy = state.copyAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 195why, one step back195 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 196why, one step back196 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
line 197why, one step back197 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 198why, one step back198 return freeze(copy)More repo setupMichel Weststratebefore adoption1637393
lines 199–200why, one step back199 }200Code cleanupMichel Weststratebefore adoption2b2d0cb
line 201why, one step back201 // create proxy for rootMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 202why, one step back202 const rootClone = createProxy(undefined, baseState)Code cleanupMichel Weststratebefore adoption2b2d0cb
line 203why, one step back203 // execute the thunkMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 204–207why, one step back204 const maybeVoidReturn = thunk(rootClone)205 //values either than undefined will trigger warning;206 !Object.is(maybeVoidReturn, undefined) &&207 console.warn(Merge pull request #21 from sallyhill/patch-1Michel Weststratebefore adoptionc8718e7
line 208why, one step back208 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 209why, one step back209 )Code cleanupMichel Weststratebefore adoption2b2d0cb
line 210why, one step back210 // and finalize the modified proxyMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 211–214why, one step back211 const res = finalize(rootClone)212 // revoke all proxies213 revocableProxies.forEach(p => p.revoke())214 return resMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 215–216why, one step back215}216Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 217–218why, one step back217function isProxy(value) {218 return !!value && !!value[PROXY_STATE]More repo setupMichel Weststratebefore adoption1637393
lines 219–220why, one step back219}220Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 221–224why, one step back221function isProxyable(value) {222 if (!value) return false223 if (typeof value !== "object") return false224 if (Array.isArray(value)) return trueMore repo setupMichel Weststratebefore adoption1637393
line 225why, one step back225 const proto = Object.getPrototypeOf(value)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 226why, one step back226 return (proto === proto) === null || Object.prototypeMore repo setupMichel Weststratebefore adoption1637393
lines 227–228why, one step back227}228Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 229why, one step back229function freeze(value) {Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0
lines 230–232why, one step back230 // short circuit to achieve 100% code coverage instead of 98%231 /*232 if(autoFreeze) {Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 233–234why, one step back233 Object.freeze(value)234 }Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0
line 235why, one step back235 * */`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 236why, one step back236 autoFreeze && Object.freeze(value)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 237–252why, one step back237 return value238}239240/**241 * Automatically freezes any state trees generated by immer.242 * This protects against accidental modifications of the state tree outside of an immer function.243 * This comes with a performance impact, so it is recommended to disable this option in production.244 * It is by default enabled.245 *246 * @returns {void}247 */248function setAutoFreeze(enableAutoFreeze) {249 autoFreeze = enableAutoFreeze250}251252Object.defineProperty(exports, "__esModule", {No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 253why, one step back253 value: trueMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 254why, one step back254})`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 255why, one step back255module.exports.default = produceMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 256why, one step back256module.exports.setAutoFreeze = setAutoFreeze