Skip to content
Myr

immer.js

This file

Path

src/immer.js

189 lines · 6.6 KiB · JavaScript

Last changed by

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

History of this file

Take it

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 1–12why, 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,12 NOTHING

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

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

1.1.0Michel Weststratebefore adoptionf735002

line 14why, one step back
14

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

1.1.0Michel Weststratebefore adoptionf735002

line 16why, one step back
16

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 26why, one step back
26}

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 27why, one step back
27

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 39why, one step back
39

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

1.1.0Michel Weststratebefore adoptionf735002

lines 43–44why, one step back
43 }
44

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

1.1.0Michel Weststratebefore adoptionf735002

line 50why, one step back
50

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 69why, one step back
69

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

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

line 73why, one step back
73

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 74–87why, one step back
74 // Finalize the modified draft...
75 if (result === undefined || result === baseDraft) {76 result = this.finalize(77 baseDraft,78 [],79 patches,80 inversePatches81 )82 }83 // ...or use a replacement value.84 else {85 // Users must never modify the draft _and_ return something else.86 if (baseDraft[DRAFT_STATE].modified)87 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 88why, one step back
88

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

line 91why, one step back
91

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 92–135why, one step back
92 if (patchListener) {
93 patches.push({94 op: "replace",95 path: [],96 value: result97 })98 inversePatches.push({99 op: "replace",100 path: [],101 value: base102 })103 }104 }105 } finally {106 this.currentScope().forEach(state => state.revoke())107 this.scopes.pop()108 }109 patchListener && patchListener(patches, inversePatches)110 }111 // Normalize the result.112 return result === NOTHING ? undefined : result113 }114 setAutoFreeze(value) {115 this.autoFreeze = value116 }117 setUseProxies(value) {118 this.useProxies = value119 assign(this, value ? modernProxy : legacyProxy)120 }121 /**122 * @internal123 * Finalize a draft, returning either the unmodified base state or a modified124 * copy of the base state.125 */126 finalize(draft, path, patches, inversePatches) {127 const state = draft[DRAFT_STATE]128 if (!state) {129 if (Object.isFrozen(draft)) return draft130 return this.finalizeTree(draft)131 }132 if (!state.modified) return state.base133 if (!state.finalized) {134 state.finalized = true135 this.finalizeTree(state.draft, path, patches, inversePatches)

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 136–141why, one step back
136 if (this.onDelete) {
137 const {assigned} = state138 for (const prop in assigned)139 assigned[prop] || this.onDelete(state, prop)140 }141 if (this.onCopy) this.onCopy(state)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 142–157why, one step back
142 if (this.autoFreeze) Object.freeze(state.copy)
143 if (patches) generatePatches(state, path, patches, inversePatches)144 }145 return state.copy146 }147 /**148 * @internal149 * Finalize all drafts in the given state tree.150 */151 finalizeTree(root, path, patches, inversePatches) {152 const state = root[DRAFT_STATE]153 if (state) {154 root = this.useProxies155 ? state.copy156 : (state.copy = shallowCopy(state.draft))157 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 158why, one step back
158

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 163why, one step back
163

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 165why, one step back
165 // prettier-ignore

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 166why, one step back
166 parent[prop] = value =

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

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

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

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

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 169–170why, one step back
169 ? this.finalize(value, path.concat(prop), patches, inversePatches)
170 : this.finalize(value)

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

lines 171–173why, one step back
171 }
172 // Unchanged base state is always ignored.173 else if (inDraft && is(value, state.base[prop])) {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

line 174why, one step back
174 return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 175why, one step back
175 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

lines 176–181why, one step back
176 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.
177 else if (isDraftable(value) && !Object.isFrozen(value)) {178 each(value, finalizeProperty)179 }180181 if (inDraft && onAssign) {

feat: add hooks to the `Immer` classaleclarsonbefore adoption7f50364

lines 182–183why, one step back
182 onAssign(state, prop, value)
183 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 184why, one step back
184 }

refactor: Immer#finalizeTree methodaleclarsonbefore adoption483a27a

line 185why, one step back
185

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 186–189why, one step back
186 each(root, finalizeProperty)
187 return root188 }189}