Skip to content
Myr

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 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 #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

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

More repo setupMichel Weststratebefore adoption1637393

lines 76–80why, 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 with

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 81why, one step back
81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified

More repo setupMichel Weststratebefore adoption1637393

lines 82–83why, one step back
82 * @returns {any} a new state, or the base state if nothing was modified
83 */

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 84–104why, one step back
84function 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 back
105 const revocableProxies = []

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 106why, one step back
106

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

lines 107–114why, one step back
107 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 back
115

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

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

More repo setupMichel Weststratebefore adoption1637393

line 132why, one step back
132 }

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

line 133why, one step back
133 }

More repo setupMichel Weststratebefore adoption1637393

line 134why, one step back
134

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

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

More repo setupMichel Weststratebefore adoption1637393

lines 145–146why, one step back
145 }
146

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

lines 147–150why, one step back
147 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 back
151

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

lines 152–168why, one step back
152 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 = true

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 169why, one step back
169 this.copy = Array.isArray(this.base)

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

line 170why, one step back
170 ? this.base.slice()

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 171why, one step back
171 : Object.assign({}, this.base) // TODO: eliminate those isArray checks?

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

lines 172–173why, one step back
172 Object.assign(this.copy, this.proxies) // yup that works for arrays as well
173 if (this.parent) this.parent.markChanged()

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

line 174why, one step back
174 }

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 175why, one step back
175 }

More repo setupMichel Weststratebefore adoption1637393

lines 176–177why, one step back
176 }
177

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

lines 178–187why, one step back
178 // creates a proxy for plain objects / arrays
179 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 back
188 }

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

lines 189–190why, one step back
189 revocableProxies.push(proxy)
190 return proxy.proxy

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

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

lines 195–201why, one step back
195 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 back
202 return base
203 }204

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

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 209why, one step back
209 })

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 211–212why, one step back
211 }
212

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

line 217why, one step back
217 })

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

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

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

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 224why, one step back
224 const maybeVoidReturn = producer(rootClone)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 229why, one step back
229 )

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

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

lines 231–234why, one step back
231 const res = finalize(rootClone)
232 // revoke all proxies233 revocableProxies.forEach(p => p.revoke())234 return res

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 235–236why, one step back
235}
236

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

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

More repo setupMichel Weststratebefore adoption1637393

lines 239–240why, one step back
239}
240

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

lines 241–244why, one step back
241function isProxyable(value) {
242 if (!value) return false243 if (typeof value !== "object") return false244 if (Array.isArray(value)) return true

More repo setupMichel Weststratebefore adoption1637393

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

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

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

More repo setupMichel Weststratebefore adoption1637393

lines 247–248why, one step back
247}
248

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

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

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

lines 250–252why, one step back
250 // 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 back
253 Object.freeze(value)
254 }

Merge pull request #25 from Gregjarvez/travisMichel Weststratebefore adoption55ec6d0

line 255why, one step back
255 * */

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 256why, one step back
256 autoFreeze && Object.freeze(value)

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

lines 257–272why, one step back
257 return value
258}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 back
273 value: true

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

line 274why, one step back
274})

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 275why, one step back
275module.exports.default = produce

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

line 276why, one step back
276module.exports.setAutoFreeze = setAutoFreeze