For example, I would like to be able to execute a(b)(c) where a, b, and c are proxy functions that I create.
So far I wrote the following piece of code:
function createProxy(funcName) {
const proxy = new Proxy(function () {}, {
apply(target, thisArg, args) {
result += `${funcName} `;
// Process and log each argument's name if it has a mapped proxy
args.forEach((arg) => {
if (proxiesMap.has(arg)) {
const argName = proxiesMap.get(arg);
result += `${argName} `;
}
});
// Return the proxy for chaining if no arguments, or pass through argument proxy if it exists
return args.length > 0 ? args[0] : proxy;
}
});
// Store the mapping from proxy to name
proxiesMap.set(proxy, funcName);
proxiedValues.add(funcName);
return proxy;
}
This is not really what I want, because if we have multiple statements that are executed on different lines, this will output:
a(b)(c);
d(e);
The above is outputting a b b c c d d e (due to appending the middle calls twice because on one hand they are function names on another hand they are arguments). The above should result in:
a b c
d e
I had another idea but it led to the same result:
function createProxy(param) {
let isFirstCall = true;
const proxy = function(...args) {
if (isFirstCall) {
result += `${param} `;
isFirstCall = false;
}
// Invoke any function arguments
args.forEach(arg => {
if (typeof arg === 'function') {
arg();
}
});
// Return the same function for chaining
return placeholder;
};
// Wrap with proxy
return new Proxy(placeholder, {
apply(target, thisArg, args) {
return target(...args);
}
});
}
Where am I going wrong in function chaining proxieS?