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
203 lines (175 loc) · 5.2 KB
/
proxify.ts
File metadata and controls
203 lines (175 loc) · 5.2 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
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 {
IDLE = 'IDLE',
TRACKING_PATHS = 'TRACKING_PATHS',
TRACKING_MUTATIONS = 'TRACKING_MUTATIONS',
}
function concat(path, prop) {
return path === undefined ? prop : path + '.' + prop
}
function shouldTrackMutations(tree, path) {
return tree.options.devmode || (path && tree.pathDependencies[path])
}
const arrayMutations = new Set([
'push',
'shift',
'pop',
'unshift',
'splice',
'reverse',
'sort',
'copyWithin',
])
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)
if (tree.status === STATUS.TRACKING_PATHS) {
tree.paths[tree.paths.length - 1].add(nestedPath)
}
if (
arrayMutations.has(String(prop)) &&
shouldTrackMutations(tree, nestedPath)
) {
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
)
}
return (...args) => {
tree.currentMutations.push({
method: prop,
path: path,
args: args,
})
return target[prop](...args)
}
}
if (target[prop] === undefined) {
return undefined
}
return (target[prop] = proxify(tree, target[prop], nestedPath))
},
set(target, prop, value) {
const nestedPath = concat(path, prop)
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
)
}
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]
}"`
)
}
tree.currentMutations.push({
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)
if (tree.status === STATUS.TRACKING_PATHS) {
tree.paths[tree.paths.length - 1].add(nestedPath)
}
if (typeof targetValue === 'function') {
return tree.options.dynamicWrapper
? tree.options.dynamicWrapper(tree, nestedPath, targetValue)
: targetValue(tree, nestedPath)
}
if (targetValue === undefined) {
return undefined
}
return (target[prop] = proxify(tree, targetValue, nestedPath))
},
set(target, prop, value) {
const nestedPath = concat(path, prop)
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
)
}
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]
}"`
)
}
if (shouldTrackMutations(tree, nestedPath)) {
if (!(prop in target)) {
tree.objectChanges.add(path)
}
tree.currentMutations.push({
method: 'set',
path: nestedPath,
args: [value],
})
}
return Reflect.set(target, prop, value)
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop)
if (tree.status !== STATUS.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
)
}
if (shouldTrackMutations(tree, nestedPath)) {
if (prop in target) {
tree.objectChanges.add(path)
}
tree.currentMutations.push({
method: 'unset',
path: nestedPath,
args: [],
})
}
delete target[prop]
return true
},
})
}
function proxify(tree, value, path?) {
if (value) {
if (value[IS_PROXY] && value[PATH] !== path) {
return proxify(tree, value[VALUE], path)
} else if (value[IS_PROXY]) {
return value
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path)
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path)
}
}
return value
}
export default proxify