GoogleChromeLabs / GoogleChromeLabs/comlink

Possible to get reference to original object from a proxy?

Open
#632 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
12.8k
Forks
435
PR merge metrics
No merged PRs in 30d

Description

Is it possible to get a reference to the original object from a proxy? [Similarly to how Immer has `original()`?](https://immerjs.github.io/immer/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:
```ts
// worker.js

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

expose(exposedApi);
```

```ts
// 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:
```ts
createSecondClass: (firstClassProxy: FirstClass & ProxyMarked) {
const FirstClassOriginal = Comlink.original(firstClassProxy);
const SecondClassInstance = new SecondClass(FirstClassOriginal)
}
```

Is it possible to do something like this?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.