-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathobjectProxyHandler.js
More file actions
41 lines (37 loc) · 1015 Bytes
/
objectProxyHandler.js
File metadata and controls
41 lines (37 loc) · 1015 Bytes
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
import client from './client'
const objectProxyHandler = {
set(target, name, value) {
if (isProxyable(name, value)) {
target[name] = new Proxy(value, this)
} else {
target[name] = value
}
if (!name.startsWith('_')) {
client.update()
}
return true
},
get(target, name, receiver) {
if (name === '_isProxy') return true
return Reflect.get(target, name, receiver)
},
}
function isProxyable(name, value) {
if (name.startsWith('_')) return false
const constructor = value?.constructor
if (!constructor) return false
if (value._isProxy) return false
return constructor === Array || constructor === Object
}
export function generateObjectProxy(name, value) {
if (isProxyable(name, value)) {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
value[key] = generateObjectProxy(key, value[key])
}
}
return new Proxy(value, objectProxyHandler)
}
return value
}
export default objectProxyHandler