#662·comlink

Feature request: Automatic proxy of arguments

Author: kubijoCreated May 24, 2024Updated Feb 6, 2025

Out of curiosity, why doesn't comlink automatically proxy callbacks out of the box or at least give an opt-in for that?

I have been able to achieve it in the following way…

typescript
function $<T>(w: Worker): Remote<T> {
    const remote = comlinkWrap<T>(w);
    return new Proxy<Remote<T>>(remote, {
        get(target, prop, receiver) {
            if (typeof target[prop] === 'function') {
                return new Proxy(target[prop], {
                    apply(target, thisArg, argArray) {
                        // Proxy functions in the argument list as well
                        const proxiedArgs = argArray.map(arg => (typeof arg === 'function' ? comlinkProxy(arg) : arg));
                        return Reflect.apply(target, thisArg, proxiedArgs);
                    },
                });
            }

            return Reflect.get(target, prop, receiver);
        },
    });
}

Am I missing something important here?

Source: GoogleChromeLabs/comlink