Skip to content
Myr

immer.js

This file

Path

src/immer.js

213 lines · 7.5 KiB · JavaScript

Last changed by

feat: limited class supportaleclarsonbefore adoption9877d64

History of this file

Take it

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–11why, one step back
1import * as legacyProxy from "./es5"
2import * as modernProxy from "./proxy"3import {generatePatches} from "./patches"4import {5 assign,6 each,7 is,8 isDraft,9 isDraftable,10 shallowCopy,11 DRAFT_STATE,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 12–14why, one step back
12 NOTHING,
13 isEnumerable,14 eachOwn

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

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

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

line 16why, one step back
16

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

line 18why, one step back
18

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 28why, one step back
28}

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

line 29why, one step back
29

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

line 41why, one step back
41

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #131 from dmorosinotto/patch_dtsMichel Weststratebefore adoption2cd32e0

line 52why, one step back
52

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 53–70why, one step back
53 let result
54 // Only create proxies for plain objects/arrays.55 if (!isDraftable(base)) {56 result = recipe(base)57 if (result === undefined) return base58 }59 // See #100, don't nest producers60 else if (isDraft(base)) {61 result = recipe.call(base, base)62 if (result === undefined) return base63 }64 // The given value must be proxied.65 else {66 this.scopes.push([])67 const baseDraft = this.createDraft(base)68 try {69 result = recipe.call(baseDraft, baseDraft)70 this.willFinalize(result, baseDraft, !!patchListener)

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 71why, one step back
71

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 72–74why, one step back
72 // Never generate patches when no listener exists.
73 var patches = patchListener && [],74 inversePatches = patchListener && []

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

line 75why, one step back
75

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 76–89why, one step back
76 // Finalize the modified draft...
77 if (result === undefined || result === baseDraft) {78 result = this.finalize(79 baseDraft,80 [],81 patches,82 inversePatches83 )84 }85 // ...or use a replacement value.86 else {87 // Users must never modify the draft _and_ return something else.88 if (baseDraft[DRAFT_STATE].modified)89 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

line 90why, one step back
90

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 91–92why, one step back
91 // Finalize the replacement in case it contains (or is) a subset of the draft.
92 if (isDraftable(result)) result = this.finalize(result)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

line 93why, one step back
93

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 94–133why, one step back
94 if (patchListener) {
95 patches.push({96 op: "replace",97 path: [],98 value: result99 })100 inversePatches.push({101 op: "replace",102 path: [],103 value: base104 })105 }106 }107 } finally {108 this.currentScope().forEach(state => state.revoke())109 this.scopes.pop()110 }111 patchListener && patchListener(patches, inversePatches)112 }113 // Normalize the result.114 return result === NOTHING ? undefined : result115 }116 setAutoFreeze(value) {117 this.autoFreeze = value118 }119 setUseProxies(value) {120 this.useProxies = value121 assign(this, value ? modernProxy : legacyProxy)122 }123 /**124 * @internal125 * Finalize a draft, returning either the unmodified base state or a modified126 * copy of the base state.127 */128 finalize(draft, path, patches, inversePatches) {129 const state = draft[DRAFT_STATE]130 if (!state) {131 if (Object.isFrozen(draft)) return draft132 return this.finalizeTree(draft)133 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 134–137why, one step back
134 // Never finalize drafts owned by an outer scope.
135 if (state.scope !== this.currentScope()) {136 return draft137 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 138–141why, one step back
138 if (!state.modified) return state.base
139 if (!state.finalized) {140 state.finalized = true141 this.finalizeTree(state.draft, path, patches, inversePatches)

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 142–147why, one step back
142 if (this.onDelete) {
143 const {assigned} = state144 for (const prop in assigned)145 assigned[prop] || this.onDelete(state, prop)146 }147 if (this.onCopy) this.onCopy(state)

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 148–154why, one step back
148
149 // Nested producers must never auto-freeze their result,150 // because it may contain drafts from parent producers.151 if (this.autoFreeze && this.scopes.length === 1) {152 Object.freeze(state.copy)153 }154

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 155–165why, one step back
155 if (patches) generatePatches(state, path, patches, inversePatches)
156 }157 return state.copy158 }159 /**160 * @internal161 * Finalize all drafts in the given state tree.162 */163 finalizeTree(root, path, patches, inversePatches) {164 const state = root[DRAFT_STATE]165 if (state) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 166–171why, one step back
166 if (!this.useProxies) {
167 state.finalizing = true168 state.copy = shallowCopy(state.draft, true)169 state.finalizing = false170 }171 root = state.copy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 172why, one step back
172 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 173why, one step back
173

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 176why, one step back
176 // Only `root` can be a draft in here.

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 178why, one step back
178

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 180why, one step back
180 value =

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 181why, one step back
181 // Patches are never generated for assigned properties.

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 182why, one step back
182 patches && inDraft && !state.assigned[prop]

feat: limited class supportaleclarsonbefore adoption9877d64

line 183why, one step back
183 ? this.finalize(value, path.concat(prop), patches, inversePatches) // prettier-ignore

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 184why, one step back
184 : this.finalize(value)

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 185why, one step back
185

feat: limited class supportaleclarsonbefore adoption9877d64

lines 186–193why, one step back
186 // Preserve non-enumerable properties.
187 if (Array.isArray(parent) || isEnumerable(parent, prop)) {188 parent[prop] = value189 } else {190 Object.defineProperty(parent, prop, {value})191 }192193 // Unchanged drafts are never passed to the `onAssign` hook.

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 195why, one step back
195 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 198why, one step back
198 return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 199why, one step back
199 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 202why, one step back
202 eachOwn(value, finalizeProperty)

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

lines 203–205why, one step back
203 }
204205 if (inDraft && onAssign) {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 206–207why, one step back
206 onAssign(state, prop, value)
207 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 208why, one step back
208 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 209why, one step back
209

feat: limited class supportaleclarsonbefore adoption9877d64

line 210why, one step back
210 eachOwn(root, finalizeProperty)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 211–213why, one step back
211 return root
212 }213}