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

async-thread-worker

v0.9.4

Published

async/await abstraction for Web Workers

Downloads

13

Readme

npm MIT licensed CI

About

async-thread-worker presents an abstraction of Web Worker thread communication based on the client-server model. Supported features are:

  • awaiting thread operations,
  • integration of basic Web Worker APIs (e.g. transferables, the terminate() method etc.), and
  • class methods/interfaces for implementing client-server style functionality.

After introducing some basic examples for quickly getting started, we demonstrate applications using Wasm binaries (C code compiled by Emscripten and Rust code by wasm-pack) embedded inside worker threads.

For other libraries that realize similar functionality, you might also consider:

Getting Started

Installation

$ npm install async-thread-worker

Usage

Here's a basic example of implementing a worker (abstracted as thread) and interacting with it. Use sendRequest() and sendResponse() for client-server style communications. [ demo | source ]

index.html: Synchronously sending requests to a worker.

// <script src='async-thread-worker.min.js'></script>

const thread = new AsyncThreadWorker.Thread('my-thread-worker.js');

for (let payload of ['a', 'b', 'c', 'd']) {
    const response = await thread.sendRequest(payload);
    console.log('[main] got response:', response);
}

my-thread-worker.js: Implementation of the worker. Use the provided id to respond to a request.

importScripts('async-thread-worker.min.js');

class MyThreadWorker extends AsyncThreadWorker.ThreadWorker {
    onRequest(id, payload) { // impl
        console.log('[worker] got request with:', payload);
        this.sendResponse(id, payload.toUpperCase());
    }
}
const myThreadWorker = new MyThreadWorker(self);

The results in the developer console:

[worker] got request: a
[main] got response: A
[worker] got request: b
[main] got response: B
[worker] got request: c
[main] got response: C
[worker] got request: d
[main] got response: D

Examples

API

AsyncThreadWorker.Thread

The Thread class is for abstraction of the main thread's side (client).

  • constructor(path) Creates a Thread object that is a worker's interface. The underlying Web Worker object wrapped by Thread is also created based on its implementation specified by path.

    • path string The path to a worker's implementation.
  • sendRequest(payload=undefined, transferables=[]) Sends a request to the worker (server) with data payload. Transferable objects can be specified in the optional transferbles array so that they are efficiently sent to the other thread without structured clone. Returns a promise corresponding to the server's action (sendResponse() or sendError()).

    • payload object | primitive e.g. 42, or {name: 'foo', input: buf}, where buf is an ArrayBuffer.
    • transferables Array<object> e.g. [buf,]
  • getWorker() Returns the raw Web Worker object wrapped by Thread (or null if the worker is already terminated).

  • terminate() Immediately terminates the worker (internally using Worker.terminate()).

AsyncThreadWorker.ThreadWorker

The ThreadWorker class is for abstraction of the worker's side (server).

  • constructor(self, opts={}) Creates a ThreadWorker object that represents the worker thread by wrapping the bare Web Worker object (self).

  • onRequest(id, payload) Called when the worker thread (server) received a request with data payload from the main thread (client). Implement this method to respond to the client by either sendResponse() or sendError().

  • sendResponse(id, payload=undefined, transferables=[]) Sends a response to the main thread (client) with data payload. Transferable objects can be specified in the optional transferbles array so that they are efficiently sent to the other thread without structured clone.

    • id string A request id provided by onRequest().
    • payload object | primitive e.g. 42, or {name: 'foo', output: buf}, where buf is an ArrayBuffer.
    • transferables Array<object> e.g. [buf,]
  • sendError(id, error) Sends an error response to the main thread (client) with data error.

    • id string A request id provided by onRequest().
    • error object | primitive
  • onCreate(opts) Called when the ThreadWorker is created. One may override this method when extending the ThreadWorker class.

    • opts object Optional parameters given to constructor().

Build

$ npm install  # set up build tools
$ npm run build