immer.js
This file
Path
immer.js
276 lines · 8.7 KiB · JavaScript
Last changed by
Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
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 #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
line 75why, one step back75 * produce takes a state, and runs a function against it.More repo setupMichel Weststratebefore adoption1637393
lines 76–80why, 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 withMerge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
line 81why, one step back81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modifiedMore repo setupMichel Weststratebefore adoption1637393
lines 82–83why, one step back82 * @returns {any} a new state, or the base state if nothing was modified83 */Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
lines 84–104why, one step back84function produce(baseState, producer) {85 // curried invocation86 if (arguments.length === 1) {87 const producer = baseState88 // prettier-ignore89 if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")90 return function() {91 const args = arguments92 return produce(args[0], draft => {93 args[0] = draft // blegh!94 baseState.apply(null, args)95 })96 }97 }9899 // prettier-ignore100 {101 if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)102 if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))103 if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")104 }Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 105why, one step back105 const revocableProxies = []Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 106why, one step back106Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 107–114why, one step back107 class State {108 constructor(parent, base) {109 this.modified = false110 this.parent = parent111 this.base = base112 this.copy = undefined113 this.proxies = {}114 }More repo setupMichel Weststratebefore adoption1637393
line 115why, one step back115Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 116–131why, one step back116 get source() {117 return this.modified === true ? this.copy : this.base118 }119120 get(prop) {121 if (this.modified) {122 const value = this.copy[prop]123 if (!isProxy(value) && isProxyable(value))124 return (this.copy[prop] = createProxy(this, value))125 return value126 } else {127 if (prop in this.proxies) return this.proxies[prop]128 const value = this.base[prop]129 if (!isProxy(value) && isProxyable(value))130 return (this.proxies[prop] = createProxy(this, value))131 return valueMore repo setupMichel Weststratebefore adoption1637393
line 132why, one step back132 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 133why, one step back133 }More repo setupMichel Weststratebefore adoption1637393
line 134why, one step back134Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 135–144why, one step back135 set(prop, value) {136 if (!this.modified) {137 if (138 (prop in this.base && this.base[prop] === value) ||139 (prop in this.proxies && this.proxies[prop] === value)140 )141 return142 this.markChanged()143 }144 this.copy[prop] = valueMore repo setupMichel Weststratebefore adoption1637393
lines 145–146why, one step back145 }146Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 147–150why, one step back147 deleteProp(prop) {148 this.markChanged()149 delete this.copy[prop]150 }Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 151why, one step back151Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 152–168why, one step back152 getOwnPropertyDescriptor(prop) {153 const owner = this.modified154 ? this.copy155 : prop in this.proxies ? this.proxies : this.base156 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)157 if (descriptor) descriptor.configurable = true // XXX: is this really needed?158 return descriptor159 }160161 defineProperty(property, descriptor) {162 this.markChanged()163 Object.defineProperty(this.copy, property, descriptor)164 }165166 markChanged() {167 if (!this.modified) {168 this.modified = trueMerge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
line 169why, one step back169 this.copy = Array.isArray(this.base)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 170why, one step back170 ? this.base.slice()Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
line 171why, one step back171 : Object.assign({}, this.base) // TODO: eliminate those isArray checks?Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 172–173why, one step back172 Object.assign(this.copy, this.proxies) // yup that works for arrays as well173 if (this.parent) this.parent.markChanged()Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 174why, one step back174 }Code cleanupMichel Weststratebefore adoption2b2d0cb
line 175why, one step back175 }More repo setupMichel Weststratebefore adoption1637393
lines 176–177why, one step back176 }177Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 178–187why, one step back178 // creates a proxy for plain objects / arrays179 function createProxy(parentState, base) {180 const state = new State(parentState, base)181 let proxy182 if (Array.isArray(base)) {183 // Proxy should be created with an array to make it an array for JS184 // so... here you have it!185 proxy = Proxy.revocable([state], arrayTraps)186 } else {187 proxy = Proxy.revocable(state, objectTraps)More repo setupMichel Weststratebefore adoption1637393
line 188why, one step back188 }Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 189–190why, one step back189 revocableProxies.push(proxy)190 return proxy.proxyMore repo setupMichel Weststratebefore adoption1637393
lines 191–192why, one step back191 }192Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 193–194why, one step back193 // given a base object, returns it if unmodified, or return the changed cloned if modified194 function finalize(base) {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 195–201why, one step back195 if (isProxy(base)) {196 const state = base[PROXY_STATE]197 if (state.modified === true) {198 if (Array.isArray(state.base)) return finalizeArray(state)199 return finalizeObject(state)200 } else return state.base201 }Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 202–204why, one step back202 return base203 }204Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 205–206why, one step back205 function finalizeObject(state) {206 const copy = state.copyMore repo setupMichel Weststratebefore adoption1637393
line 207why, one step back207 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 208why, one step back208 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
line 209why, one step back209 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 210why, one step back210 return freeze(copy)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 211–212why, one step back211 }212Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 213–214why, one step back213 function finalizeArray(state) {214 const copy = state.copyAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 215why, one step back215 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 216why, one step back216 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
line 217why, one step back217 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 218why, one step back218 return freeze(copy)More repo setupMichel Weststratebefore adoption1637393
lines 219–220why, one step back219 }220Code cleanupMichel Weststratebefore adoption2b2d0cb
line 221why, one step back221 // create proxy for rootMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 222why, one step back222 const rootClone = createProxy(undefined, baseState)Code cleanupMichel Weststratebefore adoption2b2d0cb
line 223why, one step back223 // execute the thunkMerge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e
line 224why, one step back224 const maybeVoidReturn = producer(rootClone)Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 225–227why, one step back225 //values either than undefined will trigger warning;226 !Object.is(maybeVoidReturn, undefined) &&227 console.warn(Merge pull request #21 from sallyhill/patch-1Michel Weststratebefore adoptionc8718e7
line 228why, one step back228 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 229why, one step back229 )Code cleanupMichel Weststratebefore adoption2b2d0cb
line 230why, one step back230 // and finalize the modified proxyMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 231–234why, one step back231 const res = finalize(rootClone)232 // revoke all proxies233 revocableProxies.forEach(p => p.revoke())234 return resMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 235–236why, one step back235}236Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 237–238why, one step back237function isProxy(value) {238 return !!value && !!value[PROXY_STATE]More repo setupMichel Weststratebefore adoption1637393
lines 239–240why, one step back239}240Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 241–244why, one step back241function isProxyable(value) {242 if (!value) return false243 if (typeof value !== "object") return false244 if (Array.isArray(value)) return trueMore repo setupMichel Weststratebefore adoption1637393
line 245why, one step back245 const proto = Object.getPrototypeOf(value)Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 246why, one step back246 return (proto === proto) === null || Object.prototypeMore repo setupMichel Weststratebefore adoption1637393
lines 247–248why, one step back247}248Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 249why, one step back249function freeze(value) {Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0
lines 250–252why, one step back250 // short circuit to achieve 100% code coverage instead of 98%251 /*252 if(autoFreeze) {Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 253–254why, one step back253 Object.freeze(value)254 }Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0
line 255why, one step back255 * */`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 256why, one step back256 autoFreeze && Object.freeze(value)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 257–272why, one step back257 return value258}259260/**261 * Automatically freezes any state trees generated by immer.262 * This protects against accidental modifications of the state tree outside of an immer function.263 * This comes with a performance impact, so it is recommended to disable this option in production.264 * It is by default enabled.265 *266 * @returns {void}267 */268function setAutoFreeze(enableAutoFreeze) {269 autoFreeze = enableAutoFreeze270}271272Object.defineProperty(exports, "__esModule", {No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 273why, one step back273 value: trueMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 274why, one step back274})`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 275why, one step back275module.exports.default = produceMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 276why, one step back276module.exports.setAutoFreeze = setAutoFreeze