GoogleChromeLabs / GoogleChromeLabs/comlink
Exposed function deadlocks if called too early
- Dominant language
- TypeScript
- Stars
- 12.8k
- Forks
- 435
- PR merge metrics
- No merged PRs in 30d
Description
## Problem description
Wrapped function does not return when called before the worker is ready.
## Minimum codes to reproduce
### Deps.
- comlink 4.4.1
- esbuild 0.23.0 for bundling
```ts
// task.ts: Dummy task
// task.ts
// Dummy task
export async function dummyTask(time: number) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
```
```ts
// worker.ts
import * as Comlink from "comlink";
import { dummyTask } from "./task.js";
// Wait for 500 ms
// Modeling long-running tasks (ex. WASM compilation)
await dummyTask(500);
Comlink.expose({
dummy: () => {
return 42;
},
});
```
```ts
// index.ts
import * as Comlink from "comlink";
import { dummyTask } from "./task.js";
const url = new URL("./worker.js", import.meta.url);
const wrapper: Comlink.Remote<{
dummy: () => number;
}> = Comlink.wrap(new Worker(url, { type: "module" }));
// Works without problem with this line
// await dummyTask(1000);
// Never returns
const x = await wrapper.dummy();
// Unreachable
console.log(x);
```
Contributor guide
Assessment
This issue has not been assessed yet.