forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxify.ts
More file actions
239 lines (197 loc) · 5.72 KB
/
proxify.ts
File metadata and controls
239 lines (197 loc) · 5.72 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const isPlainObject = require('is-plain-object')
export const IS_PROXY = Symbol('IS_PROXY')
export const PATH = Symbol('PATH')
export const VALUE = Symbol('VALUE')
export enum STATUS {
TRACKING_PATHS = 'TRACKING_PATHS',
TRACKING_MUTATIONS = 'TRACKING_MUTATIONS',
}
const arrayMutations = new Set([
'push',
'shift',
'pop',
'unshift',
'splice',
'reverse',
'sort',
'copyWithin',
])
function concat(path, prop) {
return path === undefined ? prop : path + '.' + prop
}
function shouldTrackMutations(tree, path) {
return tree.options.devmode || (path && tree.pathDependencies[path])
}
function ensureMutationTrackingIsEnabled(tree, path) {
if (tree.options.devmode && !tree.status.has(STATUS.TRACKING_MUTATIONS)) {
throw new Error(
`proxy-state-tree - You are mutating the path "${path}", but it is not allowed`
)
}
}
function ensureValueDosntExistInStateTreeElsewhere(value) {
if (value && value[IS_PROXY] === true) {
throw new Error(
`proxy-state-tree - You are trying to insert a value that already exists in the state tree on path "${
value[PATH]
}"`
)
}
}
function trackPath(tree, path) {
if (tree.status.has(STATUS.TRACKING_PATHS)) {
tree.paths[tree.paths.length - 1].add(path)
}
}
function createArrayProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true
if (prop === PATH) return path
if (prop === VALUE) return value
if (
prop === 'length' ||
(typeof target[prop] === 'function' &&
!arrayMutations.has(String(prop))) ||
typeof prop === 'symbol'
) {
return target[prop]
}
const nestedPath = concat(path, prop)
trackPath(tree, nestedPath)
if (
arrayMutations.has(String(prop)) &&
shouldTrackMutations(tree, nestedPath)
) {
ensureMutationTrackingIsEnabled(tree, nestedPath)
return (...args) => {
tree.addMutation({
method: prop,
path: path,
args: args,
})
return target[prop](...args)
}
}
if (target[prop] === undefined) {
return undefined
}
return proxify(tree, target[prop], nestedPath)
},
set(target, prop, value) {
const nestedPath = concat(path, prop)
ensureMutationTrackingIsEnabled(tree, nestedPath)
ensureValueDosntExistInStateTreeElsewhere(value)
tree.addMutation({
method: 'set',
path: nestedPath,
args: [value],
})
return Reflect.set(target, prop, value)
},
})
}
function createObjectProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true
if (prop === PATH) return path
if (prop === VALUE) return value
if (typeof prop === 'symbol' || prop in Object.prototype)
return target[prop]
const targetValue = target[prop]
const nestedPath = concat(path, prop)
trackPath(tree, nestedPath)
if (typeof targetValue === 'function') {
const dynamicValue = tree.options.dynamicWrapper
? tree.options.dynamicWrapper(tree, nestedPath, targetValue)
: targetValue(tree, nestedPath)
if (dynamicValue && dynamicValue[IS_PROXY]) {
delete dynamicValue[VALUE][tree.PROXY]
Object.defineProperty(dynamicValue[VALUE], tree.PROXY, {
configurable: true,
value: proxify(tree, dynamicValue[VALUE], dynamicValue[PATH]),
})
return dynamicValue[VALUE][tree.PROXY]
}
return dynamicValue
}
if (targetValue === undefined) {
return undefined
}
return proxify(tree, targetValue, nestedPath)
},
set(target, prop, value) {
const nestedPath = concat(path, prop)
ensureMutationTrackingIsEnabled(tree, nestedPath)
ensureValueDosntExistInStateTreeElsewhere(value)
if (shouldTrackMutations(tree, nestedPath)) {
if (!(prop in target)) {
tree.objectChanges.add(path)
}
tree.addMutation({
method: 'set',
path: nestedPath,
args: [value],
})
}
return Reflect.set(target, prop, value)
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop)
ensureMutationTrackingIsEnabled(tree, nestedPath)
if (shouldTrackMutations(tree, nestedPath)) {
if (prop in target) {
tree.objectChanges.add(path)
}
tree.addMutation({
method: 'unset',
path: nestedPath,
args: [],
})
}
delete target[prop]
return true
},
})
}
function getValue(value, tree) {
if (!value) {
return value
}
if (value[IS_PROXY]) {
return value[VALUE][tree.PROXY] || value[VALUE]
}
if (value[tree.PROXY]) {
return value[tree.PROXY]
}
return value
}
export function proxify(tree, value, path?) {
value = getValue(value, tree)
if (value) {
if (value[IS_PROXY] && value[PATH] !== path) {
delete value[VALUE][tree.PROXY]
Object.defineProperty(value[VALUE], tree.PROXY, {
configurable: true,
value: proxify(tree, value[VALUE], path),
})
return value[VALUE][tree.PROXY]
} else if (value[IS_PROXY]) {
return value
} else if (isPlainObject(value)) {
Object.defineProperty(value, tree.PROXY, {
configurable: true,
value: createObjectProxy(tree, value, path),
})
return value[tree.PROXY]
} else if (Array.isArray(value)) {
Object.defineProperty(value, tree.PROXY, {
configurable: true,
value: createArrayProxy(tree, value, path),
})
return value[tree.PROXY]
}
}
return value
}