forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstanceProxyHandler.js
More file actions
31 lines (29 loc) · 1.02 KB
/
instanceProxyHandler.js
File metadata and controls
31 lines (29 loc) · 1.02 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
import client from './client';
import { generateContext } from './context';
import { generateObjectProxy } from './objectProxyHandler';
const instanceProxyHandler = {
get(target, name) {
if (name === '_isProxy') return true;
if (target.constructor[name]?.name === '_invoke') return target.constructor[name].bind(target.constructor)
if (!target[name]?.name?.startsWith('_') && !name.startsWith('_') && typeof (target[name]) == 'function' && name !== 'constructor') {
const { [name]: named } = {
[name]: (args) => {
const context = generateContext({ ...target._attributes, ...args, self: target._self });
return target[name](context);
}
}
return named;
}
return Reflect.get(...arguments);
},
set(target, name, value) {
if (!value?.name?.startsWith('_') && !name.startsWith('_')) {
target[name] = generateObjectProxy(name, value);
client.update();
} else {
target[name] = value;
}
return true;
}
}
export default instanceProxyHandler;