npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

workiq

v0.0.3

Published

A fast, promise-based queue & communication layer for web workers.

Downloads

202

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 everything yet. 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.

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:

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:

import WorkerHost from "workiq";

const client = new WorkerHost({ workers, logLevel: "debug" });
client.push("getPi", {}).then((pi) => console.log(`Got pi: ${pi}`));

Transferrable objects

You may have noticed that the return value from getPi returns its value in an object. This is because functions may also return a second value containing an array of transferrable objects.

You can transfer any type of transferrable objects, including ArrayBuffers, OffscreenCanvas etc. See the full list of supported transferrable objects.

Consider the following examples:

const uInt8Array = new Uint8Array(1024 * 1024 * 8).map((v, i) => i);

// transfer the array to the worker & get a new one in return.
const newUInt8Array = await client.push("manipulateArray", uInt8Array, {
  transferList: [uInt8Array],
});

And to transfer an object back:

const manipulateArray = async (uInt8Array) => {
  // do something to the array

  // then return it as both the return value, and in the transferList
  return [uInt8Array, [uInt8Array]];
};

const workerClient = new WorkerClient({ manipulateArray });

Note that in these examples uInt8Array will not be transferred until a worker becomes free to take the job. If you try to access it after calling client.push you will hit race conditions. So it's best to treat it as alredy transferred.

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.