Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vonage/js-workerizer

Package Overview
Dependencies
Maintainers
42
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vonage/js-workerizer

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

  • 1.2.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
42
Created
Source
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.

Keywords

FAQs

Package last updated on 29 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc