Description
Specification
The threads package is quite complex and its development is stalled. It's preventing us from doing the ESM migration #12, and it's also affecting bundling and packaging in Polykey-CLI
.
It's best to replace it with our own implementation.
There are several situations we need to consider:
- In the context of nodejs, we have to use
worker_threads
- this is where we should start, as that's what the agent runs on. - In the context of browsers, we have to use WebWorkers - ensuring API consistency between the 2 is something that the libraries have been doing, but I think we can do this better.
- In the context of mobile, this depends on what the underlying system is, probably react-native so whatever threading mechanism it exposes
In order to do this, we should start by analyzing how does one define a "worker". For some reason it always ends up being a separate file to the main JS script. So when bundling something together that results in having to have 2 files at the very least. The main script and the worker script.
Is it possible to have the worker code "embedded" into the main script so one does not have to worry about the bundling problem? I think it should be doable. If we can get some way of specifying worker code that can be embedded, then when bundling occurs we don't have to worry about it. This solves deployment complexity when it comes to specifying where the worker script is, and also removes the need to have the threads or workers package be an external package, it can just be fully bundled. That's really the goal.
One nice thing would be if the worker script itself can be written in typescript. However I understand that this may overcomplicate things because it would mean a typescript compilation stage prior to bundling, we definitely do not want to embed tsc or ts-node or similar at production, so most likely it has to be written in JS-only. However IDE-wise it is possible to use jsdoc types which allows the type-checker to still check if it typed correctly, I saw this in Docusaurus.
Additional context
- feat: migrate to ESM #12
- ci: merge staging to master Polykey-CLI#4
- Spawn worker from blob / inlined bundle andywer/threads.js#211
Tasks
- ...
- ...
- ...