Skip to content
Myr

immer.js

This file

Path

immer.js

268 lines · 8.8 KiB · JavaScript

Last changed by

Simple optimization in ES5Michel Weststratebefore adoption24b5d42

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 #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 9–14why, one step back
9/**
10 * @typedef {Object} RevocableProxy11 * @property {any} proxy12 * @property {Function} revoke13 */14

WIPMichel Weststratebefore adoptiona758751

lines 15–16why, one step back
15const IS_PROXY = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version
16const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version

More repo setupMichel Weststratebefore adoption1637393

line 17why, one step back
17

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

lines 18–20why, one step back
18// 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 back
21

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

lines 22–23why, one step back
22let autoFreeze = true
23

More repo setupMichel Weststratebefore adoption1637393

line 24why, one step back
24/**

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

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

More repo setupMichel Weststratebefore adoption1637393

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

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 35–36why, one step back
35 const revocableProxies = []
36

WIPMichel Weststratebefore adoptiona758751

lines 37–38why, one step back
37 class State {
38 constructor(parent, base) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 39why, one step back
39 this.modified = false

WIPMichel Weststratebefore adoptiona758751

lines 40–41why, one step back
40 this.parent = parent
41 this.base = base

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 42why, one step back
42 this.copy = undefined

WIPMichel Weststratebefore adoptiona758751

lines 43–44why, one step back
43 this.proxies = {}
44 }

More repo setupMichel Weststratebefore adoption1637393

line 45why, one step back
45

WIPMichel Weststratebefore adoptiona758751

lines 46–50why, one step back
46 get source() {
47 return this.modified === true ? this.copy : this.base48 }4950 get(prop) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 51–62why, one step back
51 if (this.modified) {
52 const value = this.copy[prop]53 if (!isProxy(value) && isProxyable(value))54 return (this.copy[prop] = createProxy(this, value))55 return value56 } else {57 if (prop in this.proxies) return this.proxies[prop]58 const value = this.base[prop]59 if (!isProxy(value) && isProxyable(value))60 return (this.proxies[prop] = createProxy(this, value))61 return value62 }

WIPMichel Weststratebefore adoptiona758751

lines 63–66why, one step back
63 }
6465 set(prop, value) {66 if (!this.modified) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 67–70why, one step back
67 if (
68 (prop in this.base && this.base[prop] === value) ||69 (prop in this.proxies && this.proxies[prop] === value)70 )

WIPMichel Weststratebefore adoptiona758751

lines 71–79why, one step back
71 return
72 this.markChanged()73 }74 this.copy[prop] = value75 }7677 markChanged() {78 if (!this.modified) {79 this.modified = true

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 80why, one step back
80 this.copy = Array.isArray(this.base) // TODO: eliminate those isArray checks?

WIP on arraysMichel Weststratebefore adoptioncab47bb

lines 81–82why, one step back
81 ? this.base.slice()
82 : Object.assign({}, this.base)

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 83why, one step back
83 Object.assign(this.copy, this.proxies) // yup that works for arrays as well

WIPMichel Weststratebefore adoptiona758751

lines 84–88why, one step back
84 if (this.parent) this.parent.markChanged()
85 }86 }87 }88

More repo setupMichel Weststratebefore adoption1637393

lines 89–90why, one step back
89 const objectTraps = {
90 get(target, prop) {

WIPMichel Weststratebefore adoptiona758751

lines 91–93why, one step back
91 if (prop === IS_PROXY) return true
92 if (prop === PROXY_STATE) return target93 return target.get(prop)

More repo setupMichel Weststratebefore adoption1637393

lines 94–95why, one step back
94 },
95 has(target, prop) {

WIPMichel Weststratebefore adoptiona758751

line 96why, one step back
96 return prop in target.source

More repo setupMichel Weststratebefore adoption1637393

lines 97–98why, one step back
97 },
98 ownKeys(target) {

WIPMichel Weststratebefore adoptiona758751

line 99why, one step back
99 return Reflect.ownKeys(target.source)

More repo setupMichel Weststratebefore adoption1637393

lines 100–101why, one step back
100 },
101 set(target, prop, value) {

WIPMichel Weststratebefore adoptiona758751

line 102why, one step back
102 target.set(prop, value)

More repo setupMichel Weststratebefore adoption1637393

lines 103–105why, one step back
103 return true
104 },105 deleteProperty(target, property) {

WIPMichel Weststratebefore adoptiona758751

lines 106–107why, one step back
106 target.markChanged()
107 delete target.copy[property]

More repo setupMichel Weststratebefore adoption1637393

line 108why, one step back
108 return true

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 109–110why, one step back
109 },
110 getOwnPropertyDescriptor(target, prop) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 111–114why, one step back
111 // TOOD: move to state
112 const owner = target.modified113 ? target.copy114 : prop in target.proxies ? target.proxies : target.base

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 115–117why, one step back
115 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
116 if (descriptor) descriptor.configurable = true // XXX: is this really needed?117 return descriptor

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 118–119why, one step back
118 },
119 defineProperty(target, property, descriptor) {

WIPMichel Weststratebefore adoptiona758751

lines 120–121why, one step back
120 target.markChanged()
121 Object.defineProperty(target.copy, property, descriptor)

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 122–125why, one step back
122 return true
123 },124 setPrototypeOf() {125 throw new Error("Don't even try this...")

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

line 126why, one step back
126 }

More repo setupMichel Weststratebefore adoption1637393

lines 127–128why, one step back
127 }
128

WIP on arraysMichel Weststratebefore adoptioncab47bb

lines 129–149why, one step back
129 const arrayTraps = {
130 get(target, prop) {131 if (prop === IS_PROXY) return true132 if (prop === PROXY_STATE) return target[0]133 return target[0].get(prop)134 },135 has(target, prop) {136 return prop in target[0].source137 },138 ownKeys(target) {139 return Reflect.ownKeys(target[0].source)140 },141 set(target, prop, value) {142 target[0].set(prop, value)143 return true144 },145 deleteProperty(target, property) {146 target[0].markChanged()147 delete target[0].copy[property]148 return true149 },

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 150why, one step back
150 getOwnPropertyDescriptor(target_, prop) {

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

line 151why, one step back
151 // TOOD: move to state

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 152–155why, one step back
152 const target = target_[0]
153 const owner = target.modified154 ? target.copy155 : prop in target.proxies ? target.proxies : target.base

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 156–158why, one step back
156 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
157 if (descriptor) descriptor.configurable = true // XXX: is this really needed?158 return descriptor

WIP on arraysMichel Weststratebefore adoptioncab47bb

lines 159–169why, one step back
159 },
160 defineProperty(target, property, descriptor) {161 target[0].markChanged()162 Object.defineProperty(target[0].copy, property, descriptor)163 return true164 },165 setPrototypeOf() {166 throw new Error("Don't even try this...")167 }168 }169

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 170why, one step back
170 // creates a proxy for plain objects / arrays

WIPMichel Weststratebefore adoptiona758751

lines 171–172why, one step back
171 function createProxy(parentState, base) {
172 const state = new State(parentState, base)

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

line 173why, one step back
173 let proxy

WIP on arraysMichel Weststratebefore adoptioncab47bb

lines 174–176why, one step back
174 if (Array.isArray(base)) {
175 // Proxy should be created with an array to make it an array for JS176 // so... here you have it!

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

line 177why, one step back
177 proxy = Proxy.revocable([state], arrayTraps)

WIP on arraysMichel Weststratebefore adoptioncab47bb

line 178why, one step back
178 } else {

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

line 179why, one step back
179 proxy = Proxy.revocable(state, objectTraps)

WIP on arraysMichel Weststratebefore adoptioncab47bb

line 180why, one step back
180 }

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 181–182why, one step back
181 revocableProxies.push(proxy)
182 return proxy.proxy

More repo setupMichel Weststratebefore adoption1637393

lines 183–184why, one step back
183 }
184

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

lines 187–189why, one step back
187 if (isProxy(base)) {
188 const state = base[PROXY_STATE]189 if (state.modified === true) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 190–191why, one step back
190 if (isPlainObject(state.base)) return finalizeObject(state)
191 if (Array.isArray(state.base)) return finalizeArray(state)

WIPMichel Weststratebefore adoptiona758751

lines 192–193why, one step back
192 } else return state.base
193 }

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 194–196why, one step back
194 return base
195 }196

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

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

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 200why, one step back
200 copy[prop] = finalize(copy[prop]) // TODO: make sure no new proxies are generated!

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 201why, one step back
201 })

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 203–204why, one step back
203 }
204

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 205why, one step back
205 function finalizeArray(state) {

WIPMichel Weststratebefore adoptiona758751

line 206why, one step back
206 const copy = state.copy

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

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)

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 216–219why, one step back
216 const maybeVoidReturn = thunk(rootClone)
217 //values either than undefined will trigger warning;218 !Object.is(maybeVoidReturn, undefined) &&219 console.warn(

Merge pull request #21 from sallyhill/patch-1Michel Weststratebefore adoptionc8718e7

line 220why, one step back
220 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 221why, one step back
221 )

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 223–226why, one step back
223 const res = finalize(rootClone)
224 // revoke all proxies225 revocableProxies.forEach(p => p.revoke())226 return res

More repo setupMichel Weststratebefore adoption1637393

lines 227–232why, one step back
227}
228229function isPlainObject(value) {230 if (value === null || typeof value !== "object") return false231 const proto = Object.getPrototypeOf(value)232 return proto === Object.prototype || proto === null

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 233–235why, one step back
233}
234235function isProxy(value) {

WIPMichel Weststratebefore adoptiona758751

lines 236–242why, one step back
236 return !!value && !!value[IS_PROXY]
237}238239function isProxyable(value) {240 if (!value) return false241 if (typeof value !== "object") return false242 return Array.isArray(value) || isPlainObject(value)

More repo setupMichel Weststratebefore adoption1637393

lines 243–244why, one step back
243}
244

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

lines 245–264why, one step back
245function freeze(value) {
246 if (autoFreeze) {247 Object.freeze(value)248 }249 return value250}251252/**253 * Automatically freezes any state trees generated by immer.254 * This protects against accidental modifications of the state tree outside of an immer function.255 * This comes with a performance impact, so it is recommended to disable this option in production.256 * It is by default enabled.257 *258 * @returns {void}259 */260function setAutoFreeze(enableAutoFreeze) {261 autoFreeze = enableAutoFreeze262}263264Object.defineProperty(exports, "__esModule", {

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

line 265why, one step back
265 value: true

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

lines 266–268why, one step back
266})
267module.exports.default = immer268module.exports.setAutoFreeze = setAutoFreeze