1.2.0 • Published 1 year ago

connected-threads v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Connected Threads

Library that allows you to run scripts or other files on separate threads. It uses workers for managing threads, improving the performance of your applications and avoiding blocking the main thread. It has a user-friendly API, that makes it easy to create and manage threads and communicate between them.

Installation

# npm
npm i connected-threads

# yarn
yarn add connected-threads

Usage

Using functions

import { functionThread } from "connected-threads";

functionThread((a: number, b: number) => a + b, [1, 3]).then((result) => {
  console.log(result);
});

Using external scripts

index.ts

import { fileThread } from "connected-threads";

const threadEvent = fileThread<number[], number>("./file.ts");

threadEvent.on("online", () => {
  console.log("online");
});

threadEvent.postMessage([1, 2, 3]);
threadEvent.postMessage([7, 4, 7]);

threadEvent.on("message", (result: number) => {
  console.log(`Sum: ${result}`);
});

threadEvent.on("close", () => {
  console.log("close");
});

file.ts

import { getParentThread } from "connected-threads";

const parent = getParentThread<number, number[]>();

parent.on("message", (array: number[]) => {
  parent.postMessage(array.reduce((sum, val) => sum + val));
});
1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

2 years ago