Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Automatic proxy of arguments #662

Open
kubijo opened this issue May 24, 2024 · 3 comments
Open

Feature request: Automatic proxy of arguments #662

kubijo opened this issue May 24, 2024 · 3 comments

Comments

@kubijo
Copy link

kubijo commented May 24, 2024

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?

@surma
Copy link
Collaborator

surma commented May 25, 2024

It does not do it automatically because every callback requires creating a new proxy that holds on to the proxied value, which is basically a memory leak.

If you want to automatically proxy callbacks, you can use transfer handlers. The example for in the README shows how to automatically proxy events and you can use the exact same technique for functions.

@yelouafi
Copy link

yelouafi commented Jun 1, 2024

I was curious about this as well. Mainly because callbacks can’t be sent natively so we’ll always need to proxy them, which seems redundant IMO.

@kubijo
Copy link
Author

kubijo commented Jun 1, 2024

Yeah, then again... there is a strong chance for memory leaks with callbacks in general. I think I'm gonna in the end rather use some observed triggers in indexeddb (which is part of my whole setup for the project)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants