Skip to content
Myr

immer.js

This file

Path

src/immer.js

256 lines · 8.4 KiB · JavaScript

Last changed by

feat: support async recipesaleclarsonbefore adoption5fee518

History of this file

Take it

The walk stopped after 200 commits: lines older than d87d637 point at it.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–2why, one step back
1import * as legacyProxy from "./es5"
2import * as modernProxy from "./proxy"

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

line 3why, one step back
3import {applyPatches, generatePatches} from "./patches"

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 4–6why, one step back
4import {
5 assign,6 each,

fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1

line 7why, one step back
7 has,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 8–10why, one step back
8 is,
9 isDraft,10 isDraftable,

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 11why, one step back
11 isEnumerable,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 12–13why, one step back
12 shallowCopy,
13 DRAFT_STATE,

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 14why, one step back
14 NOTHING

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

line 15why, one step back
15} from "./common"

feat: support async recipesaleclarsonbefore adoption5fee518

line 16why, one step back
16import {ImmerScope} from "./scope"

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 17why, one step back
17

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 18why, one step back
18function verifyMinified() {}

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 19why, one step back
19

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 20–24why, one step back
20const configDefaults = {
21 useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",22 autoFreeze:23 typeof process !== "undefined"24 ? process.env.NODE_ENV !== "production"

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 25–28why, one step back
25 : verifyMinified.name === "verifyMinified",
26 onAssign: null,27 onDelete: null,28 onCopy: null

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 29why, one step back
29}

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 30why, one step back
30

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 31–41why, one step back
31export class Immer {
32 constructor(config) {33 assign(this, configDefaults, config)34 this.setUseProxies(this.useProxies)35 this.produce = this.produce.bind(this)36 }37 produce(base, recipe, patchListener) {38 // curried invocation39 if (typeof base === "function" && typeof recipe !== "function") {40 const defaultBase = recipe41 recipe = base

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 42why, one step back
42

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 43–45why, one step back
43 // prettier-ignore
44 return (base = defaultBase, ...args) =>45 this.produce(base, draft => recipe.call(draft, draft, ...args))

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 46–47why, one step back
46 }
47

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 48–52why, one step back
48 // prettier-ignore
49 {50 if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")51 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")52 }

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 53why, one step back
53

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 54why, one step back
54 let result

feat: support async recipesaleclarsonbefore adoption5fee518

lines 55–58why, one step back
55
56 // Only plain objects, arrays, and "immerable classes" are drafted.57 if (isDraftable(base)) {58 const scope = ImmerScope.enter()

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 59–61why, one step back
59 const baseDraft = this.createDraft(base)
60 try {61 result = recipe.call(baseDraft, baseDraft)

feat: support async recipesaleclarsonbefore adoption5fee518

lines 62–75why, one step back
62 } catch (error) {
63 scope.revoke()64 throw error65 }66 scope.leave()67 if (result instanceof Promise) {68 return result.then(69 result => {70 scope.usePatches(patchListener)71 return this.processResult(result, scope)72 },73 error => {74 scope.revoke()75 throw error

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 76why, one step back
76 }

feat: support async recipesaleclarsonbefore adoption5fee518

line 77why, one step back
77 )

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 78why, one step back
78 }

feat: support async recipesaleclarsonbefore adoption5fee518

lines 79–84why, one step back
79 scope.usePatches(patchListener)
80 return this.processResult(result, scope)81 } else {82 result = recipe(base)83 if (result === undefined) return base84 return result !== NOTHING ? result : undefined

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 85–92why, one step back
85 }
86 }87 setAutoFreeze(value) {88 this.autoFreeze = value89 }90 setUseProxies(value) {91 this.useProxies = value92 assign(this, value ? modernProxy : legacyProxy)

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

lines 93–100why, one step back
93 }
94 applyPatches(base, patches) {95 // Mutate the base state when a draft is passed.96 if (isDraft(base)) {97 return applyPatches(base, patches)98 }99 // Otherwise, produce a copy of the base state.100 return this.produce(base, draft => applyPatches(draft, patches))

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 101why, one step back
101 }

feat: support async recipesaleclarsonbefore adoption5fee518

lines 102–137why, one step back
102 /** @internal */
103 processResult(result, scope) {104 const baseDraft = scope.drafts[0]105 const isReplaced = result !== undefined && result !== baseDraft106 this.willFinalize(scope, result, isReplaced)107 if (isReplaced) {108 if (baseDraft[DRAFT_STATE].modified) {109 scope.revoke()110 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore111 }112 if (isDraftable(result)) {113 // Finalize the result in case it contains (or is) a subset of the draft.114 result = this.finalize(result, null, scope)115 }116 if (scope.patches) {117 scope.patches.push({118 op: "replace",119 path: [],120 value: result121 })122 scope.inversePatches.push({123 op: "replace",124 path: [],125 value: baseDraft[DRAFT_STATE].base126 })127 }128 } else {129 // Finalize the base draft.130 result = this.finalize(baseDraft, [], scope)131 }132 scope.revoke()133 if (scope.patches) {134 scope.patchListener(scope.patches, scope.inversePatches)135 }136 return result !== NOTHING ? result : undefined137 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 138–142why, one step back
138 /**
139 * @internal140 * Finalize a draft, returning either the unmodified base state or a modified141 * copy of the base state.142 */

feat: support async recipesaleclarsonbefore adoption5fee518

line 143why, one step back
143 finalize(draft, path, scope) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 144–146why, one step back
144 const state = draft[DRAFT_STATE]
145 if (!state) {146 if (Object.isFrozen(draft)) return draft

feat: support async recipesaleclarsonbefore adoption5fee518

line 147why, one step back
147 return this.finalizeTree(draft, null, scope)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 148why, one step back
148 }

feat: support async recipesaleclarsonbefore adoption5fee518

lines 149–150why, one step back
149 // Never finalize drafts owned by another scope.
150 if (state.scope !== scope) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 151–152why, one step back
151 return draft
152 }

feat: support async recipesaleclarsonbefore adoption5fee518

lines 153–155why, one step back
153 if (!state.modified) {
154 return state.base155 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 156–157why, one step back
156 if (!state.finalized) {
157 state.finalized = true

feat: support async recipesaleclarsonbefore adoption5fee518

lines 158–159why, one step back
158 this.finalizeTree(state.draft, path, scope)
159

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 160why, one step back
160 if (this.onDelete) {

fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1

lines 161–168why, one step back
161 // The `assigned` object is unreliable with ES5 drafts.
162 if (this.useProxies) {163 const {assigned} = state164 for (const prop in assigned) {165 if (!assigned[prop]) this.onDelete(state, prop)166 }167 } else {168 const {base, copy} = state

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 169why, one step back
169 each(base, prop => {

fix(es5): onDelete hookaleclarsonbefore adoption46cc6a1

lines 170–172why, one step back
170 if (!has(copy, prop)) this.onDelete(state, prop)
171 })172 }

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 173why, one step back
173 }

feat: support async recipesaleclarsonbefore adoption5fee518

lines 174–176why, one step back
174 if (this.onCopy) {
175 this.onCopy(state)176 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 177–179why, one step back
177
178 // Nested producers must never auto-freeze their result,179 // because it may contain drafts from parent producers.

feat: support async recipesaleclarsonbefore adoption5fee518

line 180why, one step back
180 if (this.autoFreeze && !scope.parent) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 181–183why, one step back
181 Object.freeze(state.copy)
182 }183

feat: support async recipesaleclarsonbefore adoption5fee518

lines 184–191why, one step back
184 if (path && scope.patches) {
185 generatePatches(186 state,187 path,188 scope.patches,189 scope.inversePatches190 )191 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 192–198why, one step back
192 }
193 return state.copy194 }195 /**196 * @internal197 * Finalize all drafts in the given state tree.198 */

feat: support async recipesaleclarsonbefore adoption5fee518

line 199why, one step back
199 finalizeTree(root, path, scope) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 200–201why, one step back
200 const state = root[DRAFT_STATE]
201 if (state) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 202–207why, one step back
202 if (!this.useProxies) {
203 state.finalizing = true204 state.copy = shallowCopy(state.draft, true)205 state.finalizing = false206 }207 root = state.copy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 208why, one step back
208 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 209why, one step back
209

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 210why, one step back
210 const {onAssign} = this

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 211why, one step back
211 const finalizeProperty = (prop, value, parent) => {

fix: throw on circular referencesaleclarsonbefore adoption9c6603e

lines 212–216why, one step back
212 if (value === parent) {
213 throw Error("Immer forbids circular references")214 }215216 // The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 217why, one step back
217 const inDraft = !!state && parent === root

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 218why, one step back
218

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 219why, one step back
219 if (isDraft(value)) {

feat: support async recipesaleclarsonbefore adoption5fee518

lines 220–227why, one step back
220 const needPatches =
221 inDraft && path && scope.patches && !state.assigned[prop]222223 value = this.finalize(224 value,225 needPatches ? path.concat(prop) : null,226 scope227 )

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 228why, one step back
228

feat: limited class supportaleclarsonbefore adoption9877d64

lines 229–236why, one step back
229 // Preserve non-enumerable properties.
230 if (Array.isArray(parent) || isEnumerable(parent, prop)) {231 parent[prop] = value232 } else {233 Object.defineProperty(parent, prop, {value})234 }235236 // Unchanged drafts are never passed to the `onAssign` hook.

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 237why, one step back
237 if (inDraft && value === state.base[prop]) return

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 238why, one step back
238 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 239why, one step back
239 // Unchanged draft properties are ignored.

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 240why, one step back
240 else if (inDraft && is(value, state.base[prop])) {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 241why, one step back
241 return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 242why, one step back
242 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

lines 243–244why, one step back
243 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
244 else if (isDraftable(value) && !Object.isFrozen(value)) {

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 245why, one step back
245 each(value, finalizeProperty)

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

lines 246–248why, one step back
246 }
247248 if (inDraft && onAssign) {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 249–250why, one step back
249 onAssign(state, prop, value)
250 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 251why, one step back
251 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 252why, one step back
252

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 253why, one step back
253 each(root, finalizeProperty)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 254–256why, one step back
254 return root
255 }256}