forked from koorchik/node-mole-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxify.js
More file actions
29 lines (26 loc) · 900 Bytes
/
proxify.js
File metadata and controls
29 lines (26 loc) · 900 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
function proxify(moleClient) {
const callMethodProxy = proxifyOwnMethod(moleClient.callMethod.bind(moleClient));
const notifyProxy = proxifyOwnMethod(moleClient.notify.bind(moleClient));
return new Proxy(moleClient, {
get(target, methodName) {
if (methodName === 'notify') {
return notifyProxy;
} else if (methodName === 'callMethod') {
return callMethodProxy;
} else {
return (...params) => target.callMethod.call(target, methodName, params);
}
}
});
}
function proxifyOwnMethod(ownMethod) {
return new Proxy(ownMethod, {
get(target, methodName) {
return (...params) => target.call(null, methodName, params);
},
apply(target, _, args) {
return target.apply(null, args);
}
});
}
module.exports = proxify;