Skip to content
Myr

immer.js

This file

Path

immer.js

252 lines · 7.7 KiB · JavaScript

Last changed by

Code cleanupMichel Weststratebefore adoptionfbeeade

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

WIPMichel Weststratebefore adoptiona758751

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

Code cleanupMichel Weststratebefore adoptionfbeeade

line 10why, one step back
10let autoFreeze = true

More repo setupMichel Weststratebefore adoption1637393

line 11why, one step back
11

Code cleanupMichel Weststratebefore adoptionfbeeade

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}

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 42why, one step back
42

Code cleanupMichel Weststratebefore adoptionfbeeade

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}

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

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) {

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

lines 85–86why, one step back
85 const revocableProxies = []
86

WIPMichel Weststratebefore adoptiona758751

lines 87–88why, one step back
87 class State {
88 constructor(parent, base) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

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

WIPMichel Weststratebefore adoptiona758751

lines 90–91why, one step back
90 this.parent = parent
91 this.base = base

Allmost feature complete!Michel Weststratebefore adoption82ac66c

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

WIPMichel Weststratebefore adoptiona758751

lines 93–94why, one step back
93 this.proxies = {}
94 }

More repo setupMichel Weststratebefore adoption1637393

line 95why, one step back
95

WIPMichel Weststratebefore adoptiona758751

lines 96–100why, one step back
96 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 back
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 value112 }

WIPMichel Weststratebefore adoptiona758751

lines 113–116why, one step back
113 }
114115 set(prop, value) {116 if (!this.modified) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

lines 117–120why, one step back
117 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 back
121 return
122 this.markChanged()123 }124 this.copy[prop] = value125 }126

Code cleanupMichel Weststratebefore adoptionfbeeade

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

WIPMichel Weststratebefore adoptiona758751

lines 146–148why, one step back
146 markChanged() {
147 if (!this.modified) {148 this.modified = true

Allmost feature complete!Michel Weststratebefore adoption82ac66c

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

WIP on arraysMichel Weststratebefore adoptioncab47bb

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

Allmost feature complete!Michel Weststratebefore adoption82ac66c

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

WIPMichel Weststratebefore adoptiona758751

lines 153–157why, one step back
153 if (this.parent) this.parent.markChanged()
154 }155 }156 }157

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

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

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

line 161why, one step back
161 let proxy

WIP on arraysMichel Weststratebefore adoptioncab47bb

lines 162–164why, one step back
162 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 back
165 proxy = Proxy.revocable([state], arrayTraps)

WIP on arraysMichel Weststratebefore adoptioncab47bb

line 166why, one step back
166 } else {

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

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

WIP on arraysMichel Weststratebefore adoptioncab47bb

line 168why, one step back
168 }

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

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) {

WIPMichel Weststratebefore adoptiona758751

lines 175–177why, one step back
175 if (isProxy(base)) {
176 const state = base[PROXY_STATE]177 if (state.modified === true) {

Allmost feature complete!Michel Weststratebefore adoption82ac66c

line 178why, one step back
178 if (Array.isArray(state.base)) return finalizeArray(state)

Code cleanupMichel Weststratebefore adoptionfbeeade

line 179why, one step back
179 return finalizeObject(state)

WIPMichel Weststratebefore adoptiona758751

lines 180–181why, one step back
180 } else return state.base
181 }

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 182–184why, one step back
182 return base
183 }184

WIPMichel Weststratebefore adoptiona758751

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 adoptionfbeeade

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

Allmost feature complete!Michel Weststratebefore adoption82ac66c

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

WIPMichel Weststratebefore adoptiona758751

line 194why, one step back
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

WIPMichel Weststratebefore adoptiona758751

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

Fixed remaining failing testsMichel Weststratebefore adoption4e7f44a

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

More repo setupMichel Weststratebefore adoption1637393

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 217why, one step back
217function isProxy(value) {

Code cleanupMichel Weststratebefore adoptionfbeeade

line 218why, one step back
218 return !!value && !!value[PROXY_STATE]

WIPMichel Weststratebefore adoptiona758751

lines 219–223why, one step back
219}
220221function isProxyable(value) {222 if (!value) return false223 if (typeof value !== "object") return false

Code cleanupMichel Weststratebefore adoptionfbeeade

lines 224–226why, one step back
224 if (Array.isArray(value)) return true
225 const proto = Object.getPrototypeOf(value)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

lines 229–248why, one step back
229function 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 back
249 value: true

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

lines 250–252why, one step back
250})
251module.exports.default = immer252module.exports.setAutoFreeze = setAutoFreeze