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

Possible to get reference to original object from a proxy? #632

Open
ivancuric opened this issue Mar 13, 2023 · 2 comments
Open

Possible to get reference to original object from a proxy? #632

ivancuric opened this issue Mar 13, 2023 · 2 comments

Comments

@ivancuric
Copy link

ivancuric commented Mar 13, 2023

Is it possible to get a reference to the original object from a proxy? Similarly to how Immer has original()?

I am working with some delegate patterns from a WASM codebase and need to create 2 class instances, where the second one is taking the first one as a constructor parameter.

So basically this:

// worker.js

const exposedApi = {
    FirstClass: proxy(wasmModule.FirstClass),
    SecondClass: proxy(wasmModule.SecondClass)
}

expose(exposedApi);
// main.js

const wrappedWorker = wrap(worker);

const FirstClassProxyInstance = await new wrappedWorker.FirstClass(...params);

// we make some changes
await FistClassProxy.someMethod(...params)`

// and try to create the second class
const SecondClassProxyInstance = await new wrappedWorker.SecondClass(FirstClassProxyInstance)

How I see it, I should not expose the raw SecondClass, but a helper method which would implement a theoretical Comlink.original that takes the proxy from one thread and gets the original:

createSecondClass: (firstClassProxy: FirstClass & ProxyMarked) {
   const FirstClassOriginal = Comlink.original(firstClassProxy);
   const SecondClassInstance = new SecondClass(FirstClassOriginal)
}

Is it possible to do something like this?

@ivancuric ivancuric changed the title Possible to get reference to original object from a proxy? Possible to get reference to original object from a proxy (Remote)? Mar 15, 2023
@ivancuric ivancuric changed the title Possible to get reference to original object from a proxy (Remote)? Possible to get reference to original object from a proxy (ProxyMarked)? Mar 15, 2023
@ivancuric ivancuric changed the title Possible to get reference to original object from a proxy (ProxyMarked)? Possible to get reference to original object from a proxy? Mar 16, 2023
@ghost
Copy link

ghost commented Mar 17, 2023

I have another propose - Comlink.weakProxy(obj) (similar with C++ weak_ptr) or Comlink.reference(obj) which means, when will transfer back, will referenced back. Also such type can be transferred into proxy, and from another side. Doing reference database, and object which points into that reference by uuid. When transfers back, ID is checking and backing reference.

@ivancuric
Copy link
Author

Here's an example:

https://stackblitz.com/edit/vitejs-vite-7jnhy8?file=src%2Fmain.ts&file=src%2Fworker.ts

Is there any way to make this scenario work?

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

1 participant