Since you can't pass a library or function to the run function, is there a way to import it inside the run function?
I tried this:
Worker.run(async (batchPatch) => {
const { useBatchesApi } = await import("@/hooks/useBatches")
const { update } = useBatchesApi()
return update(batchPatch)
}, [batchPatch])
.then(console.log)
.catch(console.error)
But then I get Failed to resolve module specifier '/src/hooks/useBatches.ts?t=1649663469345'. I've been trying for hours now just to make the update method work. Please tell me there's a way to do this.
Let me start off stating that web workers built inline do not play well with many build tools. E.g. i had WebPack attempt to polyfill pieces of code which does not work out well, same with imports handled by the compiler.
Importing ES2015 modules from your worker is possible with the underlying workers nowadays by adding type: module to the worker https://stackoverflow.com/questions/44118600/web-workers-how-to-import-modules
However this package does not have that at the moment and i doubt it's a good idea to add it considering build tools will probably attempt to compile this instead of the browser.
The other option might be the importScripts
In either case this means this import you want to use must be splitted and accessible for your browser to download.
Do take what i say with a lot of salt since i've only used and done my research and used web workers for about a week, so definitely check on your own as well