immer.js
This file
Path
immer.js
252 lines · 7.7 KiB · JavaScript
Last changed by
Code cleanupMichel Weststratebefore adoptionfbeeade
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 )8WIPMichel Weststratebefore adoptiona758751
line 9why, one step back9const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer versionCode cleanupMichel Weststratebefore adoptionfbeeade
line 10why, one step back10let autoFreeze = trueMore repo setupMichel Weststratebefore adoption1637393
line 11why, one step back11Code cleanupMichel Weststratebefore adoptionfbeeade
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}Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 42why, one step back42Code cleanupMichel Weststratebefore adoptionfbeeade
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}Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
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–84why, 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 */84function immer(baseState, thunk) {Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a
lines 85–86why, one step back85 const revocableProxies = []86WIPMichel Weststratebefore adoptiona758751
lines 87–88why, one step back87 class State {88 constructor(parent, base) {Allmost feature complete!Michel Weststratebefore adoption82ac66c
line 89why, one step back89 this.modified = falseWIPMichel Weststratebefore adoptiona758751
lines 90–91why, one step back90 this.parent = parent91 this.base = baseAllmost feature complete!Michel Weststratebefore adoption82ac66c
line 92why, one step back92 this.copy = undefinedWIPMichel Weststratebefore adoptiona758751
lines 93–94why, one step back93 this.proxies = {}94 }More repo setupMichel Weststratebefore adoption1637393
line 95why, one step back95WIPMichel Weststratebefore adoptiona758751
lines 96–100why, one step back96 get source() {97 return this.modified === true ? this.copy : this.base98 }99100 get(prop) {Allmost feature complete!Michel Weststratebefore adoption82ac66c
lines 101–112why, one step back101 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 value112 }WIPMichel Weststratebefore adoptiona758751
lines 113–116why, one step back113 }114115 set(prop, value) {116 if (!this.modified) {Allmost feature complete!Michel Weststratebefore adoption82ac66c
lines 117–120why, one step back117 if (118 (prop in this.base && this.base[prop] === value) ||119 (prop in this.proxies && this.proxies[prop] === value)120 )WIPMichel Weststratebefore adoptiona758751
lines 121–126why, one step back121 return122 this.markChanged()123 }124 this.copy[prop] = value125 }126Code cleanupMichel Weststratebefore adoptionfbeeade
lines 127–145why, one step back127 deleteProp(prop) {128 this.markChanged()129 delete this.copy[prop]130 }131132 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 }145WIPMichel Weststratebefore adoptiona758751
lines 146–148why, one step back146 markChanged() {147 if (!this.modified) {148 this.modified = trueAllmost feature complete!Michel Weststratebefore adoption82ac66c
line 149why, one step back149 this.copy = Array.isArray(this.base) // TODO: eliminate those isArray checks?WIP on arraysMichel Weststratebefore adoptioncab47bb
lines 150–151why, one step back150 ? this.base.slice()151 : Object.assign({}, this.base)Allmost feature complete!Michel Weststratebefore adoption82ac66c
line 152why, one step back152 Object.assign(this.copy, this.proxies) // yup that works for arrays as wellWIPMichel Weststratebefore adoptiona758751
lines 153–157why, one step back153 if (this.parent) this.parent.markChanged()154 }155 }156 }157Code cleanupMichel Weststratebefore adoption2b2d0cb
line 158why, one step back158 // creates a proxy for plain objects / arraysWIPMichel Weststratebefore adoptiona758751
lines 159–160why, one step back159 function createProxy(parentState, base) {160 const state = new State(parentState, base)Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a
line 161why, one step back161 let proxyWIP on arraysMichel Weststratebefore adoptioncab47bb
lines 162–164why, one step back162 if (Array.isArray(base)) {163 // Proxy should be created with an array to make it an array for JS164 // so... here you have it!Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a
line 165why, one step back165 proxy = Proxy.revocable([state], arrayTraps)WIP on arraysMichel Weststratebefore adoptioncab47bb
line 166why, one step back166 } else {Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a
line 167why, one step back167 proxy = Proxy.revocable(state, objectTraps)WIP on arraysMichel Weststratebefore adoptioncab47bb
line 168why, one step back168 }Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a
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) {WIPMichel Weststratebefore adoptiona758751
lines 175–177why, one step back175 if (isProxy(base)) {176 const state = base[PROXY_STATE]177 if (state.modified === true) {Allmost feature complete!Michel Weststratebefore adoption82ac66c
line 178why, one step back178 if (Array.isArray(state.base)) return finalizeArray(state)Code cleanupMichel Weststratebefore adoptionfbeeade
line 179why, one step back179 return finalizeObject(state)WIPMichel Weststratebefore adoptiona758751
lines 180–181why, one step back180 } else return state.base181 }Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 182–184why, one step back182 return base183 }184WIPMichel Weststratebefore adoptiona758751
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 adoptionfbeeade
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 }192Allmost feature complete!Michel Weststratebefore adoption82ac66c
line 193why, one step back193 function finalizeArray(state) {WIPMichel Weststratebefore adoptiona758751
line 194why, one step back194 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 rootWIPMichel Weststratebefore adoptiona758751
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 proxyFixed remaining failing testsMichel Weststratebefore adoption4e7f44a
lines 211–214why, one step back211 const res = finalize(rootClone)212 // revoke all proxies213 revocableProxies.forEach(p => p.revoke())214 return resMore repo setupMichel Weststratebefore adoption1637393
lines 215–216why, one step back215}216Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 217why, one step back217function isProxy(value) {Code cleanupMichel Weststratebefore adoptionfbeeade
line 218why, one step back218 return !!value && !!value[PROXY_STATE]WIPMichel Weststratebefore adoptiona758751
lines 219–223why, one step back219}220221function isProxyable(value) {222 if (!value) return false223 if (typeof value !== "object") return falseCode cleanupMichel Weststratebefore adoptionfbeeade
lines 224–226why, one step back224 if (Array.isArray(value)) return true225 const proto = Object.getPrototypeOf(value)226 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
lines 229–248why, one step back229function freeze(value) {230 if (autoFreeze) {231 Object.freeze(value)232 }233 return value234}235236/**237 * Automatically freezes any state trees generated by immer.238 * This protects against accidental modifications of the state tree outside of an immer function.239 * This comes with a performance impact, so it is recommended to disable this option in production.240 * It is by default enabled.241 *242 * @returns {void}243 */244function setAutoFreeze(enableAutoFreeze) {245 autoFreeze = enableAutoFreeze246}247248Object.defineProperty(exports, "__esModule", {No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 249why, one step back249 value: trueMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 250–252why, one step back250})251module.exports.default = immer252module.exports.setAutoFreeze = setAutoFreeze