-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
immer.js
191 lines (178 loc) · 6.86 KB
/
immer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as legacyProxy from "./es5"
import * as modernProxy from "./proxy"
import {generatePatches} from "./patches"
import {
assign,
each,
is,
isDraft,
isDraftable,
shallowCopy,
DRAFT_STATE,
NOTHING
} from "./common"
function verifyMinified() {}
const configDefaults = {
useProxies: typeof Proxy !== "undefined" && typeof Reflect !== "undefined",
autoFreeze:
typeof process !== "undefined"
? process.env.NODE_ENV !== "production"
: verifyMinified.name === "verifyMinified",
onAssign: null,
onDelete: null,
onCopy: null
}
export class Immer {
constructor(config) {
assign(this, configDefaults, config)
this.setUseProxies(this.useProxies)
this.produce = this.produce.bind(this)
}
produce(base, recipe, patchListener) {
// curried invocation
if (typeof base === "function" && typeof recipe !== "function") {
const defaultBase = recipe
recipe = base
// prettier-ignore
return (base = defaultBase, ...args) =>
this.produce(base, draft => recipe.call(draft, draft, ...args))
}
// prettier-ignore
{
if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")
if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")
}
let result
// Only create proxies for plain objects/arrays.
if (!isDraftable(base)) {
result = recipe(base)
if (result === undefined) return base
}
// See #100, don't nest producers
else if (isDraft(base)) {
result = recipe.call(base, base)
if (result === undefined) return base
}
// The given value must be proxied.
else {
this.scopes.push([])
const baseDraft = this.createDraft(base)
try {
result = recipe.call(baseDraft, baseDraft)
this.willFinalize(result, baseDraft, !!patchListener)
// Never generate patches when no listener exists.
var patches = patchListener && [],
inversePatches = patchListener && []
// Finalize the modified draft...
if (result === undefined || result === baseDraft) {
result = this.finalize(
baseDraft,
[],
patches,
inversePatches
)
}
// ...or use a replacement value.
else {
// Users must never modify the draft _and_ return something else.
if (baseDraft[DRAFT_STATE].modified)
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
// Finalize the replacement in case it contains (or is) a subset of the draft.
if (isDraftable(result)) result = this.finalize(result)
if (patchListener) {
patches.push({
op: "replace",
path: [],
value: result
})
inversePatches.push({
op: "replace",
path: [],
value: base
})
}
}
} finally {
this.currentScope().forEach(state => state.revoke())
this.scopes.pop()
}
patchListener && patchListener(patches, inversePatches)
}
// Normalize the result.
return result === NOTHING ? undefined : result
}
setAutoFreeze(value) {
this.autoFreeze = value
}
setUseProxies(value) {
this.useProxies = value
assign(this, value ? modernProxy : legacyProxy)
}
/**
* @internal
* Finalize a draft, returning either the unmodified base state or a modified
* copy of the base state.
*/
finalize(draft, path, patches, inversePatches) {
const state = draft[DRAFT_STATE]
if (!state) {
if (Object.isFrozen(draft)) return draft
return this.finalizeTree(draft)
}
if (!state.modified) return state.base
if (!state.finalized) {
state.finalized = true
this.finalizeTree(state.draft, path, patches, inversePatches)
if (this.onDelete) {
const {assigned} = state
for (const prop in assigned)
assigned[prop] || this.onDelete(state, prop)
}
if (this.onCopy) this.onCopy(state)
if (this.autoFreeze) Object.freeze(state.copy)
if (patches) generatePatches(state, path, patches, inversePatches)
}
return state.copy
}
/**
* @internal
* Finalize all drafts in the given state tree.
*/
finalizeTree(root, path, patches, inversePatches) {
const state = root[DRAFT_STATE]
if (state) {
root = this.useProxies
? state.copy
: (state.copy = shallowCopy(state.draft))
}
const {onAssign} = this
const finalizeProperty = (prop, value, parent) => {
// Only `root` can be a draft in here.
const inDraft = state && parent === root
if (isDraftable(value)) {
// Skip unchanged properties in draft objects.
if (inDraft && is(value, state.base[prop])) return
// Find proxies within new objects. Frozen objects are already finalized.
if (!isDraft(value)) {
if (!Object.isFrozen(value)) each(value, finalizeProperty)
if (onAssign && inDraft) onAssign(state, prop, value)
return
}
// prettier-ignore
parent[prop] =
// Patches are never generated for assigned properties.
patches && parent === root && !(state && state.assigned[prop])
? this.finalize(value, path.concat(prop), patches, inversePatches)
: this.finalize(value)
if (onAssign && inDraft) onAssign(state, prop, parent[prop])
return
}
// Call `onAssign` for primitive values.
if (onAssign && inDraft && !is(value, state.base[prop])) {
onAssign(state, prop, value)
}
}
each(root, finalizeProperty)
return root
}
}