GoogleChromeLabs / GoogleChromeLabs/comlink

Comlink hangs indefinitely when loading in an async worker context that it hasn't loaded

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

Description

During a transpilation event using webpack, a file named the injector is created, because my code is splitted, and my worker code shares with the app code, the injector loads asynchronously the content of these files.

eg in my app code.

`const fancyWorker = wrap(new Worker("/resources/fancyworker.injector.js"))`

The injector then loads asynchronously

`resources/commons.js` and `resources/fancyworker.js` where commons contains code in common and fancyworker contains the whatever is specific to the worker.

if I then do before all the others are loaded:

`await fancyWorker.myMethod()`

It will just hang forever.

This doesn't occur when using sync importScripts, but only when loading async using fetch; this is a very hard to debug error as, unless you call a function at once and not by user input, everything will work out normal, until your network is slow and at the same time the cache was invalidated.

While this as of now is an edge case, I expect that as webpack keeps developing forwards the use of async loading in workers becomes more commonplace.

I'd consider an option to pass an alternate worker eg...

`const fancyWorker = wrap(new ComlinkManuallyActivatedWorker(new Worker("/resources/fancyworker.injector.js")))`

And in my fancy injector once I know that it's ready and accepting.

`comlink.ready()`

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.