GoogleChromeLabs / GoogleChromeLabs/comlink

Cannot pass in an object with a proxy callback

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

Description

I have an object where I want to have each of the functions be proxied back to the parent, but when passing in that object it tries to serialize the proxied functions rather than just passing them along.

Mini repro:

worker.js
```
importScripts("https://unpkg.com/comlink@alpha/dist/umd/comlink.js");
// importScripts("../../../dist/umd/comlink.js");

async function remoteFunction(obj) {
await obj.cb("A string from a worker");
}

Comlink.expose(remoteFunction);
```

index.html
```

import * as Comlink from "https://unpkg.com/comlink@alpha/dist/esm/comlink.mjs";
// import * as Comlink from "../../../dist/esm/comlink.mjs";

function callback(value) {
alert(`Result: ${value}`);
}

async function init() {
const remoteFunction = Comlink.wrap(new Worker("worker.js"));
const obj = { cb: Comlink.proxy(callback) }
await remoteFunction(obj);
}

init();

```

The only work around I've found right now is passing all the functions in as a `...arg` and then remapping them to their original functions back on the webworker.

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.