threadosaurus

A simple and intuitive way to utilize worker threads in Node.js with TypeScript.

MIT License

Downloads
257
Stars
1

threadosaurus

install

npm i threadosaurus

Usage

Here's a quick example of how to use Threadosaurus:

import { CreateThreadosaurus } from 'threadosaurus';
import SampleWorkerThreadClass from './SampleWorkerThreadClass';

const worker = CreateThreadosaurus(SampleWorkerThreadClass);
console.log(await worker.greet('LJ and NJ'));

In SampleWorkerThreadClass.ts:

import { Threadosaurus } from 'threadosaurus';

export default class SampleWorkerThreadClass implements Threadosaurus {
    async greet(name: string): Promise<string> {
        return Promise.resolve(`Hello ${name} from worker thread!`);
    }

    get__filename(): string {
        return __filename;
    }
}

Restrictions

When using Threadosaurus, please keep the following limitations in mind:

  • The worker class must implement the Threadosaurus interface.
  • The worker class must be the default export.
  • The worker class cannot accept constructor arguments.
  • The worker class will not retain member variables across method calls from the main thread.
  • All methods in the worker class must be asynchronous.
  • All method arguments in the worker class must be worker threads compatible types.
    More here.
  • It is recommended to design your class methods as pure functions for better predictability.

License

MIT