'Proxy trap for a part of functions
I implemented a proxy handler to intercept all functions in a class.
class SBFinder {
foo1({ name, id }) {
console.log(name + id);
}
foo2({ name, id }) {
console.log(id + name);
}
}
const handler = {
get(target, propKey, receiver) {
const targetValue = Reflect.get(target, propKey, receiver);
if (typeof targetValue === 'function') {
return (...args) => {
console.log('before');
const res = targetValue.apply(this, args);
console.log('after');
return res;
};
}
return targetValue;
},
};
const finderProxy = new Proxy(new SBFinder(), handler);
finderProxy.foo1({ name: 'name1', id: 223 });
Now I want the handler to be applied only to a part of funstions. I understand that it's quite easy to implement according to a function name, number of arguments etc. But I don't want to limit the user by a naming conventions. Is there a way to do it for example according to some JSDocs tag (like we do with annotations in java). Or maybe there's another practice you can advice?
Solution 1:[1]
can you be more specific in what you want to do?
In your get handler you are getting the class methods ("functions") but you should not call the function here.
if you return a wrapper function then you should make sure that you bind the correct object (which is the target not this) and you will want some logic to evaluate the args or the method name.
The desired method name is what you have referred to a propKey. So you could have some logic that examines propKey.
Protip -- if you are going to do tests against the value of propKey then watch out that propKey could be a symbol so you might have to add a check for when propKey is a string (e.g. a regex test will auto-cast to string)
Note also that you could return a proxy for the function also like this:
const fnProxyMaker = (fn)=>new Proxy( fn, {
apply(target, that, args){
// do something before calling or whatever
return target(...args);
}
});
const handler = {
get(target, propKey, receiver) {
const targetValue = Reflect.get(target, propKey, receiver);
if (typeof targetValue === 'function' && propKey === 'foo1') {
return fnProxyMaker(targetValue)
}
return targetValue;
},
};
or define the methods as proxies in the class:
class SBFinder {
constructor(){
['foo1','foo2'].forEach(method=>
Object.defineProperty(this, method, {
get : fnProxyMaker( ({name,id})=>console.log(name+id) )
}), this )
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gillyspy |
