@vonage/js-workerizer
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published
Vonage logo

js-workerizer

Library providing a simple way to run a class instance on its dedicated worker.

Table of content

Installation

Requirements

Installation

# Install dependencies
npm install @vonage/js-workerizer

Running example

# from the example/basic directory
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(); // run on the main thread

const onAWorker = await workerize(MyWorkerizableClass, Worker);
await onMainThread.doSomething(); // run on a worker

Run a class in a worker

Imaging you want to run this class in a worker.

export class HugeProcessClass {
    run() {
        // ... super slow process
    }
}

// Class usage
const process = new HugeProcessClass();
process.run(); // will run on the main thread
  1. 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 {
    // ... class impl
    
    // These lines allow the class to be used in a worker
    static {
        registerWorker('HugeProcessClass', this);
    }
}
  1. Second, you need to instantiate the class as a worker using workerize function instead of new operator.
// Class usage
const process = await workerize(HugeProcessClass);
await process.run(); // will run on a worker

Terminate a worker

export class SomeClass {    
    // this method will be call if defined before the worker is terminated
    public terminate() {
    }
    
    // These lines allow the class to be used in a worker
    static {
        registerWorker('HugeProcessClass', this);
    }
}


// Class usage
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

Readme

Keywords

Package Sidebar

Install

npm i @vonage/js-workerizer

Weekly Downloads

419

Version

1.2.3

License

MIT

Unpacked Size

101 kB

Total Files

48

Last publish

Collaborators

  • aviadhouri.va
  • keichenblat
  • daniel-sapir
  • ekatz
  • llihovodov
  • tabdullah
  • jmoramunoz2
  • sailusha
  • hilakl
  • leppelin
  • molszewski_v
  • twilliams253
  • abolles
  • lallen2
  • yonatan.kra
  • jtiet
  • kpaxian
  • vonage-frontier
  • vonage_client_media_processing
  • or.cpc
  • jeffswartz
  • rhainer
  • deliajolt
  • raulkg
  • liranbin
  • javiermolsanz
  • v-kpheng
  • vgai-dev
  • web-il
  • iceberg-team
  • yinon
  • unified_portal
  • vreporter-npm
  • vbcbe
  • voxip_team
  • yuri.guller
  • idanvon
  • nexmo-devrel
  • vvd
  • vonagemeetings
  • vonage-jenkins
  • maikthomas
  • germangol
  • arivonage