js-workerizer
Library providing a simple way to run a class instance on its dedicated worker.
Table of content
Installation
Requirements
Installation
npm install @vonage/js-workerizer
Running example
npm install
npm run dev
Quick start
Short example
import { MyWorkerizableClass } from "./my-workerizable-class.ts";
import Worker from "./my-workerizable-class.ts?worker&inline";
const onMainThread = new MyWorkerizableClass();
onMainThread.doSomething();
const onAWorker = await workerize(MyWorkerizableClass, Worker);
await onMainThread.doSomething();
Run a class in a worker
Imaging you want to run this class in a worker.
export class HugeProcessClass {
run() {
}
}
const process = new HugeProcessClass();
process.run();
- First step is to make the class instantiable from the worker.
To achieve it, you must use the registerWorker function.
We suggest to call it in the static constructor of the class.
Decorator should be provided soon as typescript release them.
export class HugeProcessClass {
static {
registerWorker('HugeProcessClass', this);
}
}
- Second, you need to instantiate the class as a worker using workerize function instead of new operator.
const process = await workerize(HugeProcessClass);
await process.run();
Terminate a worker
export class SomeClass {
public terminate() {
}
static {
registerWorker('HugeProcessClass', this);
}
}
const worker = await workerize(SomeClass);
await worker.terminate();
License
This project is licensed under the terms of the MIT license and is available for free.
Links