Skip to content
Myr

immer.js

This file

Path

immer.js

256 lines · 7.8 KiB · JavaScript

Last changed by

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

History of this file

Take it

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

lines 1–3why, one step back
1"use strict"
2// @ts-check3

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 4–8why, one step back
4if (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 )8

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 9–10why, one step back
9const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version
10let autoFreeze = true

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 11why, one step back
11

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 12–41why, one step back
12const 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 back
42

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 43–72why, one step back
43const 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 back
73

More repo setupMichel Weststratebefore adoption1637393

line 74why, one step back
74/**

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

line 75why, one step back
75 * Immer takes a state, and runs a function against it.

More repo setupMichel Weststratebefore adoption1637393

lines 76–84why, one step back
76 * 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) {

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 85why, one step back
85 const revocableProxies = []

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 86why, one step back
86

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 87–94why, one step back
87 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 back
95

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 96–111why, one step back
96 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 value

More repo setupMichel Weststratebefore adoption1637393

line 112why, one step back
112 }

No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31

line 113why, one step back
113 }

More repo setupMichel Weststratebefore adoption1637393

line 114why, one step back
114

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 115–124why, one step back
115 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] = value

More repo setupMichel Weststratebefore adoption1637393

lines 125–126why, one step back
125 }
126

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 127–130why, one step back
127 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 back
131

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 132–153why, one step back
132 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 back
154 }

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 155why, one step back
155 }

More repo setupMichel Weststratebefore adoption1637393

lines 156–157why, one step back
156 }
157

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 158–167why, one step back
158 // creates a proxy for plain objects / arrays
159 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 back
168 }

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 169–170why, one step back
169 revocableProxies.push(proxy)
170 return proxy.proxy

More repo setupMichel Weststratebefore adoption1637393

lines 171–172why, one step back
171 }
172

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 173–174why, one step back
173 // given a base object, returns it if unmodified, or return the changed cloned if modified
174 function finalize(base) {

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 175–181why, one step back
175 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 back
182 return base
183 }184

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 185–186why, one step back
185 function finalizeObject(state) {
186 const copy = state.copy

More repo setupMichel Weststratebefore adoption1637393

line 187why, one step back
187 Object.keys(copy).forEach(prop => {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 188why, one step back
188 copy[prop] = finalize(copy[prop])

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 189why, one step back
189 })

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 190why, one step back
190 return freeze(copy)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 191–192why, one step back
191 }
192

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 193–194why, one step back
193 function finalizeArray(state) {
194 const copy = state.copy

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 195why, one step back
195 copy.forEach((value, index) => {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 196why, one step back
196 copy[index] = finalize(copy[index])

More repo setupMichel Weststratebefore adoption1637393

line 197why, one step back
197 })

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 198why, one step back
198 return freeze(copy)

More repo setupMichel Weststratebefore adoption1637393

lines 199–200why, one step back
199 }
200

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 201why, one step back
201 // create proxy for root

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 202why, one step back
202 const rootClone = createProxy(undefined, baseState)

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 203why, one step back
203 // execute the thunk

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 204–207why, one step back
204 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 back
208 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 209why, one step back
209 )

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 210why, one step back
210 // and finalize the modified proxy

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 211–214why, one step back
211 const res = finalize(rootClone)
212 // revoke all proxies213 revocableProxies.forEach(p => p.revoke())214 return res

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 215–216why, one step back
215}
216

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 217–218why, one step back
217function isProxy(value) {
218 return !!value && !!value[PROXY_STATE]

More repo setupMichel Weststratebefore adoption1637393

lines 219–220why, one step back
219}
220

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 221–224why, one step back
221function isProxyable(value) {
222 if (!value) return false223 if (typeof value !== "object") return false224 if (Array.isArray(value)) return true

More repo setupMichel Weststratebefore adoption1637393

line 225why, one step back
225 const proto = Object.getPrototypeOf(value)

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 226why, one step back
226 return (proto === proto) === null || Object.prototype

More repo setupMichel Weststratebefore adoption1637393

lines 227–228why, one step back
227}
228

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 229why, one step back
229function freeze(value) {

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

lines 230–232why, one step back
230 // 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 back
233 Object.freeze(value)
234 }

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

lines 235–236why, one step back
235 * */
236 autoFreeze && Object.freeze(value);

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

lines 237–252why, one step back
237 return value
238}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 back
253 value: true

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

lines 254–256why, one step back
254})
255module.exports.default = immer256module.exports.setAutoFreeze = setAutoFreeze