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

client-workerpool

v1.0.1

Published

Client worker pool, used to establish communication between worker threads in a closed cluster pool, to load balance tasks running in the background (like encoding, compression).

Downloads

8

Readme

client-workerpool

This library maintains a pool of workers.

Each worker runs on a separate background thread, on the client device (web browser).

Each worker in the pool communicates with other siblings, using a private two-way MessageChannel.

Each worker in the pool is loaded from the same JavaScript source, and so they all have the same registered commands. When executing a registered command, a promise is returned.

The communication to sibling threads is load balanced from the perspective of the worker or device sending the message.

Usage

Two interfaces need to be implemented. One on the device (browser), and one on the background thread (worker).

The device uses the WorkerPool to communicate with background threads. Each background thread uses the ThreadPool to register commands and communicate with siblings.

On The Client Device (Browser)

The client device instantiates a WorkerPool, and specifies the number of threads to spawn. Using this object, the client device can communicate with workers in the ThreadPool.

/* index.js */

// import the worker pool 
import { WorkerPool } from 'client-workerpool/lib/WorkerPool';

// import your worker (this example uses the worker-loader for webpack)
import MyWorker from 'worker-loader!./my-worker.js';

// start the pool with the default number of threads, and pass your worker class to it
const pool = new WorkerPool(MyWorker, 5);

// execute a command on a worker in the ThreadPool and wait for the response asynchronously
pool.sendCommand('doSomething', ['arg', 'list']).then(data => {
    console.log('got response from workers', data);
}).catch(err => {
    console.error(err.stack);
});

On The Background Thread (Worker)

The worker uses the ThreadPool to register and provide business logic for commands recieved from the device.

/* my-worker.js */

// import the thread pool class
import { ThreadPool } from 'client-workerpool/lib/ThreadPool';

// create your thread pool instance, and pass the worker context to it (`self`)
const thread = new ThreadPool(self);

// register commands on this thread
thread.registerCommand('doSomething', (threadId, arg, list) => {

    // threadId is the thread making the request
    // arg, list are the arguments passed to the command
    
    // ask other threads to perform some background tasks for us
    return Promise.all([
        
        thread.sendCommand('doBackgroundStuff', [arg, list]),
        thread.sendCommand('doBackgroundStuff', [arg, list]),
        thread.sendCommand('doBackgroundStuff', [arg, list]),
        thread.sendCommand('doBackgroundStuff', [arg, list])
        
    ]).then(data => { 
        
        // resovle with your final data payload - this gets sent back to the main requesting thread (master)
        return data;
        
    });
    
});

// register the background command here also
thread.registerCommand('doBackgroundStuff', (threadId, arg, list) => {

    const data = [];
    const limit = Math.random() * 1000000;
    for (let i = 0; i < limit; i++) {
        data.push(Math.random() * i);
    }
    
    return data;

});

ThreadPool Public Interface

The ThreadPool has a public interface that provides business logic for common commands.

Be sure to examine all the classes and mixins.

HTTP Requests

To make an HTTP request from within a worker pool, use the ThreadPool.httpRequest method.

Installation

npm install --save client-workerpool