Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ashkyd/workiq
A library for managing web processes inside web workers
https://github.com/ashkyd/workiq
Last synced: about 1 month ago
JSON representation
A library for managing web processes inside web workers
- Host: GitHub
- URL: https://github.com/ashkyd/workiq
- Owner: AshKyd
- License: isc
- Created: 2024-11-06T07:40:41.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-08T05:43:40.000Z (about 2 months ago)
- Last Synced: 2024-11-08T06:29:00.827Z (about 2 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Workiq
A fast, promise-based queue & communication layer for web workers.
This library provides a `WorkerClient` for use inside a web worker, and `WorkerHost` for managing many web workers. Jobs are added to the queue and sent to the next available worker.
This library is in early development and doesn't yet implement things like cancelling jobs and transferrable data. However it is a good layer for simple worker communication.
## Usage
In your worker, set up as many functions as you require. Functions should accept one object as a parameter and must be async/return a promise. Objects can contain simple data types such as strings, booleans, objects, numbers, arrays, and can return any of the same.
```js
import WorkerClient from "workiq";const getPi = async () => Math.PI;
const workerClient = new WorkerClient({ getPi });
```Next, create as many workers as you require for your task. In this example we're creating a worker for each thread on the system:
```js
const workers = Array.from({
length: window.navigator.hardwareConcurrency - 1,
}).map(
() =>
new Worker(new URL("./demoworker.js", import.meta.url), {
type: "module",
})
);
```Finally, create the worker host to manage the workers. Call `push` to push jobs onto the queue, corresponding to the functions in your worker:
```js
import WorkerHost from "workiq";const client = new WorkerHost({ workers, logLevel: "debug" });
client.push("getPi", {}).then((pi) => console.log(`Got pi: ${pi}`));
```## Development
- Development uses native ES modules. There is no build step.
- Typescript types are provided along with inline documentation using JSDoc.
- You're welcome to fork this library and add your own functionality. This repo is not currently accepting PRs unless otherwise agreed, but I'm happy to have a look at your work and merge it back if it matches the vague future architecture I've got in my head.