Worker Link
Opinionated library to work with threads in Node.js.
Attention: This library is in heavy development, you should not use it in production.
Prologue
I really like the Elixir way to deal with processes and concurrent communication. But I really love Node and all JS community. And I love so much types and resilient programming. This is the reason to creating this library.
The worker-link
library is a set of classes and functions to write JavaScript classes that can run in parallel. Yes, parallel. This library is built on top of Node.js worker_threads
module.
Getting Started
npm install worker-link
Example with TypeScript
// fib-worker.ts;
// index.ts;; for ; i < 8; i++
Remember: you need to compile first because the decorator @worker
uses the __filename
injected reference. The code uses all threads of an AMD Ryzen 7 processor:
Example with JavaScript
// ./simple-worker.jsconst worker WorkerJob = ; { console; this; } moduleexports = SimpleWorker;
Then create another file:
// ./index.jsconst spawn = ;const SimpleWorker = ; const sw = ; sw;
Then run with node:
$ node index.js
You should see the following message in your terminal:
$ node index.js receive data: hello!received from thread: OK!
You terminal will be stuck at here, because the thread is running and waiting for more messages. To end a process you need to call the exit
method when the promise terminate:
// ./index.jsconst spawn = ;const SimpleWorker = ; const sw = ; sw ;
Now run the node index.js
again and your terminal will be released after the thread reply:
$ node index.js receive data: hello!received from thread: OK! $
How it works?
All thread-classes should extend of the WorkerJob class because this class implements the reply
method, used to respond when a given message is received.
In TypeScript you can decorate the thread-class with the worker
function, or you can compose it, as the way that it works with JavaScript.
When you spawn a thread-class the method send
is exposed and you can send any type of messages to that class and wait for the response. The send
method returns a Promise, so it's easy to handle the thread result.
All spawned thread-class does not terminate when respond to a given message, if you need to terminate a thread, you can call the method exit
.
Roadmap
- Spawn classes as threads.
- Spawn classes wrapped in supervisors.
- Spawn supervisors wrapped in supervisors creating the supervisor tree.
- Better Typescript support