Comlink hangs indefinitely when loading in an async worker context that it hasn't loaded
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<MyFancyWorker>(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<MyFancyWorker>(new ComlinkManuallyActivatedWorker(new Worker("/resources/fancyworker.injector.js")))
And in my fancy injector once I know that it's ready and accepting.
comlink.ready()
I am getting this issue as well, and cannot find a workaround.
I somehow found a workaround for this last year and regretfully did not document how I did it. I have to refactor the code now and I am running into this problem again.