-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
proxy.js
169 lines (150 loc) · 4.74 KB
/
proxy.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
"use strict"
// @ts-check
import {
assign,
each,
has,
is,
isDraftable,
isDraft,
shallowCopy,
DRAFT_STATE
} from "./common"
// For nested produce calls:
export const scopes = []
export const currentScope = () => scopes[scopes.length - 1]
// Do nothing before being finalized.
export function willFinalize() {}
export function createDraft(base, parent) {
const state = {
// Track which produce call this is associated with.
scope: parent ? parent.scope : currentScope(),
// True for both shallow and deep changes.
modified: false,
// Used during finalization.
finalized: false,
// Track which properties have been assigned (true) or deleted (false).
assigned: {},
// The parent draft state.
parent,
// The base state.
base,
// The base proxy.
draft: null,
// Any property proxies.
drafts: {},
// The base copy with any updated values.
copy: null,
// Called by the `produce` function.
revoke: null
}
const {revoke, proxy} = Array.isArray(base)
? Proxy.revocable([state], arrayTraps)
: Proxy.revocable(state, objectTraps)
state.draft = proxy
state.revoke = revoke
state.scope.push(state)
return proxy
}
const objectTraps = {
get,
has(target, prop) {
return prop in source(target)
},
ownKeys(target) {
return Reflect.ownKeys(source(target))
},
set,
deleteProperty,
getOwnPropertyDescriptor,
defineProperty() {
throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore
},
getPrototypeOf(target) {
return Object.getPrototypeOf(target.base)
},
setPrototypeOf() {
throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore
}
}
const arrayTraps = {}
each(objectTraps, (key, fn) => {
arrayTraps[key] = function() {
arguments[0] = arguments[0][0]
return fn.apply(this, arguments)
}
})
arrayTraps.deleteProperty = function(state, prop) {
if (isNaN(parseInt(prop))) {
throw new Error("Immer only supports deleting array indices") // prettier-ignore
}
return objectTraps.deleteProperty.call(this, state[0], prop)
}
arrayTraps.set = function(state, prop, value) {
if (prop !== "length" && isNaN(parseInt(prop))) {
throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore
}
return objectTraps.set.call(this, state[0], prop, value)
}
function source(state) {
return state.copy || state.base
}
function get(state, prop) {
if (prop === DRAFT_STATE) return state
let {drafts} = state
// Check for existing draft in unmodified state.
if (!state.modified && has(drafts, prop)) {
return drafts[prop]
}
const value = source(state)[prop]
if (state.finalized || !isDraftable(value)) return value
// Check for existing draft in modified state.
if (state.modified) {
// Assigned values are never drafted. This catches any drafts we created, too.
if (value !== state.base[prop]) return value
// Store drafts on the copy (when one exists).
drafts = state.copy
}
return (drafts[prop] = createDraft(value, state))
}
function set(state, prop, value) {
if (!state.modified) {
// Optimize based on value's truthiness. Truthy values are guaranteed to
// never be undefined, so we can avoid the `in` operator. Lastly, truthy
// values may be drafts, but falsy values are never drafts.
const isUnchanged = value
? is(state.base[prop], value) || value === state.drafts[prop]
: is(state.base[prop], value) && prop in state.base
if (isUnchanged) return true
markChanged(state)
}
state.assigned[prop] = true
state.copy[prop] = value
return true
}
function deleteProperty(state, prop) {
// The `undefined` check is a fast path for pre-existing keys.
if (state.base[prop] !== undefined || prop in state.base) {
state.assigned[prop] = false
markChanged(state)
}
if (state.copy) delete state.copy[prop]
return true
}
function getOwnPropertyDescriptor(state, prop) {
const owner = source(state)
const desc = Reflect.getOwnPropertyDescriptor(owner, prop)
if (desc) {
desc.writable = true
desc.configurable = !Array.isArray(owner) || prop !== "length"
}
return desc
}
function markChanged(state) {
if (!state.modified) {
state.modified = true
state.copy = assign(shallowCopy(state.base), state.drafts)
state.drafts = null
if (state.parent) markChanged(state.parent)
}
}