Skip to content

Feature request: Automatic proxy of arguments #662

@kubijo

Description

@kubijo

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…

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions