Find a file
Anton Nesterov ae3881975c
Some checks failed
Publish / publish (push) Has been cancelled
minor improvements; fix: error handler
2024-10-26 19:36:47 +02:00
.github/workflows publish 2024-10-25 23:25:38 +02:00
__test__ init 2024-10-25 23:19:39 +02:00
.gitignore init 2024-10-25 23:19:39 +02:00
bun.lockb init 2024-10-25 23:19:39 +02:00
index.js minor improvements; fix: error handler 2024-10-26 19:36:47 +02:00
jsr.json minor improvements; fix: error handler 2024-10-26 19:36:47 +02:00
LICENSE init 2024-10-25 23:19:39 +02:00
mod.ts init 2024-10-25 23:19:39 +02:00
offload.error.ts satisfy export-docs 2024-10-25 23:31:48 +02:00
offload.handler.ts satisfy export-docs 2024-10-25 23:31:48 +02:00
offload.interface.ts satisfy export-docs 2024-10-25 23:31:48 +02:00
offload.ts minor improvements; fix: error handler 2024-10-26 19:36:47 +02:00
package.json minor improvements; fix: error handler 2024-10-26 19:36:47 +02:00
README.md minor improvements; fix: error handler 2024-10-26 19:36:47 +02:00
tsconfig.json init 2024-10-25 23:19:39 +02:00

Offload

Offload heavy tasks using WebWorkers.

Offload creates a limited execution pool and can operate in two modes:

  • callback mode (default) - spawns web workers on demand when the function is called, self terminated
  • background - runs workers as backround jobs, distributes load among them, must be terminated manually

Currently uses unstable WebWorkers API.

Install:

bun add githib:nesterow/offload # or pnpm

Usage

Considering following worker:

// print.worker.ts
import { handler } from "@nesterow/offload";
declare var self: Worker;

handler(async (data: string) => {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  console.log(data);
  return true;
});

Callback operation mode

In the callback mode, print() will spawn a worker and terminate it after the task is done. Maximum of 5 workers may be spawned at the same time, the rest will be queued:

import { offload } from "@nesterow/offload";

const [print, terminate] = offload<boolean, string>("./print.worker.ts", 5);

await print("Hello, World!"); // => true

Callback operatinal mode us useful when thread startup delay doesn't matter. You don't need to worry about worker termination as it exits after the callback returns result. This is default "safe" option as it allows to call offload in any part of the application.

Background operation mode

In the background mode, offload will spawn 5 workers, print() will distribute the tasks among them:

import { offload } from "@nesterow/offload";

const [print, terminate] = offload<boolean, string>("./print.worker.ts", 5, 'bg');

await print("Hello, World!"); // => true

terminate(); // terminate all workers, for example on exit signal

Background operation mode is useful when you need to spawn pre-defined number of the threads on application start. Generally it is more effective as it balances the load among the threads and doesn't have startup delay.

Types

Because offload doesn't know params and return types of your worker, you need to pass type arguments manually:

const [callback, termiate] = offload<ReturnType, ParamType>("./my.worker.ts", 1);

const param: ParamType = {};
const result: ReturnType = await callback(param: ParamType);

License

MIT