immer.js
This file
Path
immer.js
220 lines · 6.7 KiB · JavaScript
Last changed by
WIPMichel Weststratebefore adoptiona758751
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 #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 9–14why, one step back9/**10 * @typedef {Object} RevocableProxy11 * @property {any} proxy12 * @property {Function} revoke13 */14WIPMichel Weststratebefore adoptiona758751
lines 15–16why, one step back15const IS_PROXY = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version16const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer versionMore repo setupMichel Weststratebefore adoption1637393
line 17why, one step back17Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 18–20why, one step back18// This property indicates that the current object is cloned for another object,19// to make sure the proxy of a frozen object is writeable20const CLONE_TARGET = Symbol("immer-clone-target")Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 21why, one step back21Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 22–23why, one step back22let autoFreeze = true23More repo setupMichel Weststratebefore adoption1637393
line 24why, one step back24/**Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f
line 25why, one step back25 * Immer takes a state, and runs a function against it.More repo setupMichel Weststratebefore adoption1637393
lines 26–34why, one step back26 * That function can freely mutate the state, as it will create copies-on-write.27 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned28 *29 * @export30 * @param {any} baseState - the state to start with31 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified32 * @returns {any} a new state, or the base state if nothing was modified33 */34function immer(baseState, thunk) {WIPMichel Weststratebefore adoptiona758751
lines 35–45why, one step back35 class State {36 // /** @type {boolean} */37 // modified38 // /** @type {State} */39 // parent40 // /** @type {any} */41 // base42 // /** @type {any} */43 // copy44 // /** @type {any} */45 // proxiesMerge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 46why, one step back46WIPMichel Weststratebefore adoptiona758751
lines 47–52why, one step back47 constructor(parent, base) {48 this.modified49 this.parent = parent50 this.base = base51 this.proxies = {}52 }More repo setupMichel Weststratebefore adoption1637393
line 53why, one step back53WIPMichel Weststratebefore adoptiona758751
lines 54–85why, one step back54 get source() {55 return this.modified === true ? this.copy : this.base56 }5758 get(prop) {59 const proxy = this.proxies[prop]60 if (proxy) return proxy61 const value = this.source[prop]62 if (!isProxy(value) && isProxyable(value))63 return (this.proxies[prop] = createProxy(this, value))64 return value65 }6667 set(prop, value) {68 if (!this.modified) {69 if (this.proxies[prop] === value || this.base[prop] === value)70 return71 this.markChanged()72 }73 if (isProxy(value)) this.proxies[prop] = value74 this.copy[prop] = value75 }7677 markChanged() {78 if (!this.modified) {79 this.modified = true80 this.copy = Object.assign({}, this.base)81 if (this.parent) this.parent.markChanged()82 }83 }84 }85More repo setupMichel Weststratebefore adoption1637393
lines 86–87why, one step back86 const objectTraps = {87 get(target, prop) {WIPMichel Weststratebefore adoptiona758751
lines 88–90why, one step back88 if (prop === IS_PROXY) return true89 if (prop === PROXY_STATE) return target90 return target.get(prop)More repo setupMichel Weststratebefore adoption1637393
lines 91–92why, one step back91 },92 has(target, prop) {WIPMichel Weststratebefore adoptiona758751
line 93why, one step back93 return prop in target.sourceMore repo setupMichel Weststratebefore adoption1637393
lines 94–95why, one step back94 },95 ownKeys(target) {WIPMichel Weststratebefore adoptiona758751
line 96why, one step back96 return Reflect.ownKeys(target.source)More repo setupMichel Weststratebefore adoption1637393
lines 97–98why, one step back97 },98 set(target, prop, value) {WIPMichel Weststratebefore adoptiona758751
line 99why, one step back99 target.set(prop, value)More repo setupMichel Weststratebefore adoption1637393
lines 100–102why, one step back100 return true101 },102 deleteProperty(target, property) {WIPMichel Weststratebefore adoptiona758751
lines 103–104why, one step back103 target.markChanged()104 delete target.copy[property]More repo setupMichel Weststratebefore adoption1637393
line 105why, one step back105 return trueMerge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 106–107why, one step back106 },107 getOwnPropertyDescriptor(target, prop) {WIPMichel Weststratebefore adoptiona758751
line 108why, one step back108 return Reflect.getOwnPropertyDescriptor(target.source, prop)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 109–110why, one step back109 },110 defineProperty(target, property, descriptor) {WIPMichel Weststratebefore adoptiona758751
lines 111–112why, one step back111 target.markChanged()112 Object.defineProperty(target.copy, property, descriptor)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 113–116why, one step back113 return true114 },115 setPrototypeOf() {116 throw new Error("Don't even try this...")No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 117why, one step back117 }More repo setupMichel Weststratebefore adoption1637393
lines 118–119why, one step back118 }119Code cleanupMichel Weststratebefore adoption2b2d0cb
line 120why, one step back120 // creates a proxy for plain objects / arraysWIPMichel Weststratebefore adoptiona758751
lines 121–123why, one step back121 function createProxy(parentState, base) {122 const state = new State(parentState, base)123 return new Proxy(state, objectTraps)More repo setupMichel Weststratebefore adoption1637393
lines 124–125why, one step back124 }125Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 126–127why, one step back126 // given a base object, returns it if unmodified, or return the changed cloned if modified127 function finalize(base) {WIPMichel Weststratebefore adoptiona758751
lines 128–134why, one step back128 if (isProxy(base)) {129 const state = base[PROXY_STATE]130 if (state.modified === true) {131 if (isPlainObject(base)) return finalizeObject(state)132 if (Array.isArray(base)) return finalizeArray(state)133 } else return state.base134 }Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 135–137why, one step back135 return base136 }137WIPMichel Weststratebefore adoptiona758751
lines 138–139why, one step back138 function finalizeObject(state) {139 const copy = state.copyMore repo setupMichel Weststratebefore adoption1637393
line 140why, one step back140 Object.keys(copy).forEach(prop => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 141why, one step back141 copy[prop] = finalize(copy[prop])Add support for arraysMichel Weststratebefore adoptiond3696cb
line 142why, one step back142 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 143why, one step back143 return freeze(copy)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 144–146why, one step back144 }145146 function finalizeArray(thing) {WIPMichel Weststratebefore adoptiona758751
line 147why, one step back147 const copy = state.copyAdd support for arraysMichel Weststratebefore adoptiond3696cb
line 148why, one step back148 copy.forEach((value, index) => {Code cleanupMichel Weststratebefore adoption2b2d0cb
line 149why, one step back149 copy[index] = finalize(copy[index])More repo setupMichel Weststratebefore adoption1637393
line 150why, one step back150 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 151why, one step back151 return freeze(copy)More repo setupMichel Weststratebefore adoption1637393
lines 152–153why, one step back152 }153Code cleanupMichel Weststratebefore adoption2b2d0cb
line 154why, one step back154 // create proxy for rootWIPMichel Weststratebefore adoptiona758751
line 155why, one step back155 const rootClone = createProxy(undefined, baseState)Code cleanupMichel Weststratebefore adoption2b2d0cb
line 156why, one step back156 // execute the thunkMerge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
lines 157–160why, one step back157 const maybeVoidReturn = thunk(rootClone)158 //values either than undefined will trigger warning;159 !Object.is(maybeVoidReturn, undefined) &&160 console.warn(Merge pull request #21 from sallyhill/patch-1Michel Weststratebefore adoptionc8718e7
line 161why, one step back161 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 162why, one step back162 )Added performance measurements, fixes #6Michel Weststratebefore adoption553299f
line 163why, one step back163 // console.log(`proxies: ${revocableProxies.size}, copies: ${copies.size}`)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 164why, one step back164 // revoke all proxiesWIPMichel Weststratebefore adoptiona758751
line 165why, one step back165 // TODO: revoke(revocableProxies)Code cleanupMichel Weststratebefore adoption2b2d0cb
line 166why, one step back166 // and finalize the modified proxyWIPMichel Weststratebefore adoptiona758751
line 167why, one step back167 return finalize(rootClone)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 168–171why, one step back168}169170/**171 * Revoke all the proxies stored in the revocableProxies mapMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 172why, one step back172 *Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 173–174why, one step back173 * @param {Map<Object,RevocableProxy>} revocableProxies174 */Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 175why, one step back175function revoke(revocableProxies) {Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 176–178why, one step back176 for (var revocableProxy of revocableProxies.values()) {177 revocableProxy.revoke()178 }More repo setupMichel Weststratebefore adoption1637393
lines 179–184why, one step back179}180181function isPlainObject(value) {182 if (value === null || typeof value !== "object") return false183 const proto = Object.getPrototypeOf(value)184 return proto === Object.prototype || proto === nullFixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 185–187why, one step back185}186187function isProxy(value) {WIPMichel Weststratebefore adoptiona758751
lines 188–194why, one step back188 return !!value && !!value[IS_PROXY]189}190191function isProxyable(value) {192 if (!value) return false193 if (typeof value !== "object") return false194 return Array.isArray(value) || isPlainObject(value)More repo setupMichel Weststratebefore adoption1637393
lines 195–196why, one step back195}196Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 197–216why, one step back197function freeze(value) {198 if (autoFreeze) {199 Object.freeze(value)200 }201 return value202}203204/**205 * Automatically freezes any state trees generated by immer.206 * This protects against accidental modifications of the state tree outside of an immer function.207 * This comes with a performance impact, so it is recommended to disable this option in production.208 * It is by default enabled.209 *210 * @returns {void}211 */212function setAutoFreeze(enableAutoFreeze) {213 autoFreeze = enableAutoFreeze214}215216Object.defineProperty(exports, "__esModule", {No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 217why, one step back217 value: trueMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 218–220why, one step back218})219module.exports.default = immer220module.exports.setAutoFreeze = setAutoFreeze