WorkTank
A simple isomorphic library for executing functions inside WebWorkers or Node Threads pools.
Features
- Small: It's about as small as you can make it.
- Isomorphic: It transparently uses WebWorkers if they are available, otherwise it uses Node's
worker_threads
module. - Dynamic pools: You can create pools dynamically, just by passing serializable functions to the library at run time, without needing any bundler plugins at all.
- Electron-ready: Electron's special renderer process environment is supported out of the box too.
- TypeScript-ready: Types come with the library and aren't an afterthought.
Install
npm install --save worktank
Usage
First you have to make a worker pool:
import WorkTank from 'worktank';
const pool = new WorkTank ({
size: 5,
methods: {
sum: function ( a: number, b: number ): number {
const math = this.require ( 'math' )
return math.sum ( a + b );
},
foo: () => {},
bar: () => {}
}
});
Then you call exec
on the pool, to call the method that you want inside the first available worker:
const result = await pool.exec (
'sum',
[10, 5]
);
console.log ( result );
Lastly once you are done you can call terminate
to end all the worker threads the pool spawned and free up some memory, if you call exec
on the pool again after having called terminate
on it the needed worker threads will be spawned up again:
pool.terminate ();
License
MIT © Fabio Spampinato