Skip to content
Myr

immer.js

This file

Path

immer.js

220 lines · 6.7 KiB · JavaScript

Last changed by

WIPMichel Weststratebefore adoptiona758751

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

WIPMichel Weststratebefore adoptiona758751

lines 35–45why, one step back
35 class State {
36 // /** @type {boolean} */37 // modified38 // /** @type {State} */39 // parent40 // /** @type {any} */41 // base42 // /** @type {any} */43 // copy44 // /** @type {any} */45 // proxies

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 46why, one step back
46

WIPMichel Weststratebefore adoptiona758751

lines 47–52why, one step back
47 constructor(parent, base) {
48 this.modified49 this.parent = parent50 this.base = base51 this.proxies = {}52 }

More repo setupMichel Weststratebefore adoption1637393

line 53why, one step back
53

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

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

WIPMichel Weststratebefore adoptiona758751

lines 88–90why, one step back
88 if (prop === IS_PROXY) return true
89 if (prop === PROXY_STATE) return target90 return target.get(prop)

More repo setupMichel Weststratebefore adoption1637393

lines 91–92why, one step back
91 },
92 has(target, prop) {

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

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

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

lines 97–98why, one step back
97 },
98 set(target, prop, value) {

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

lines 100–102why, one step back
100 return true
101 },102 deleteProperty(target, property) {

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

line 105why, one step back
105 return true

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

lines 106–107why, one step back
106 },
107 getOwnPropertyDescriptor(target, prop) {

WIPMichel Weststratebefore adoptiona758751

line 108why, one step back
108 return Reflect.getOwnPropertyDescriptor(target.source, prop)

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

lines 109–110why, one step back
109 },
110 defineProperty(target, property, descriptor) {

WIPMichel Weststratebefore adoptiona758751

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

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

lines 113–116why, one step back
113 return true
114 },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 back
117 }

More repo setupMichel Weststratebefore adoption1637393

lines 118–119why, one step back
118 }
119

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

lines 121–123why, one step back
121 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 back
124 }
125

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

lines 128–134why, one step back
128 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 back
135 return base
136 }137

WIPMichel Weststratebefore adoptiona758751

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 142why, one step back
142 })

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 144–146why, one step back
144 }
145146 function finalizeArray(thing) {

WIPMichel Weststratebefore adoptiona758751

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

line 150why, one step back
150 })

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

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

More repo setupMichel Weststratebefore adoption1637393

lines 152–153why, one step back
152 }
153

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 162why, one step back
162 )

Added performance measurements, fixes #6Michel Weststratebefore adoption553299f

line 163why, one step back
163 // console.log(`proxies: ${revocableProxies.size}, copies: ${copies.size}`)

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 164why, one step back
164 // revoke all proxies

WIPMichel Weststratebefore adoptiona758751

line 165why, one step back
165 // TODO: revoke(revocableProxies)

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

WIPMichel Weststratebefore adoptiona758751

line 167why, one step back
167 return finalize(rootClone)

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 168–171why, one step back
168}
169170/**171 * Revoke all the proxies stored in the revocableProxies map

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

line 172why, one step back
172 *

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 173–174why, one step back
173 * @param {Map<Object,RevocableProxy>} revocableProxies
174 */

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

line 175why, one step back
175function revoke(revocableProxies) {

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 176–178why, one step back
176 for (var revocableProxy of revocableProxies.values()) {
177 revocableProxy.revoke()178 }

More repo setupMichel Weststratebefore adoption1637393

lines 179–184why, one step back
179}
180181function isPlainObject(value) {182 if (value === null || typeof value !== "object") return false183 const proto = Object.getPrototypeOf(value)184 return proto === Object.prototype || proto === null

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 185–187why, one step back
185}
186187function isProxy(value) {

WIPMichel Weststratebefore adoptiona758751

lines 188–194why, one step back
188 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 back
195}
196

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

lines 197–216why, one step back
197function 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 back
217 value: true

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

lines 218–220why, one step back
218})
219module.exports.default = immer220module.exports.setAutoFreeze = setAutoFreeze