-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
41 lines (36 loc) · 1.34 KB
/
index.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
const proxymise = (target) => {
if (typeof target === 'object') {
const proxy = () => target;
proxy.__proxy__ = true;
return new Proxy(proxy, handler);
}
return typeof target === 'function' ? new Proxy(target, handler) : target;
};
const handler = {
construct(target, argumentsList) {
if (target.__proxy__) target = target();
return proxymise(Reflect.construct(target, argumentsList));
},
get(target, property, receiver) {
if (target.__proxy__) target = target();
if (property !== 'then' && property !== 'catch' && typeof target.then === 'function') {
return proxymise(target.then(value => get(value, property, receiver)));
}
return proxymise(get(target, property, receiver));
},
apply(target, thisArg, argumentsList) {
if (target.__proxy__) target = target();
if (typeof target.then === 'function') {
return proxymise(target.then(value => Reflect.apply(value, thisArg, argumentsList)));
}
return proxymise(Reflect.apply(target, thisArg, argumentsList));
}
};
const get = (target, property, receiver) => {
const value = typeof target === 'object' ? Reflect.get(target, property, receiver) : target[property];
if (typeof value === 'function' && typeof value.bind === 'function') {
return Object.assign(value.bind(target), value);
}
return value;
};
module.exports = proxymise;