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 🙏

© 2025 – Pkg Stats / Ryan Hefner

workers-manager

v1.0.3

Published

Web worker manager.

Readme

workers-manager

NPM version Clean Code Dependency Status devDependency Status License

Web worker manager ( Can be used both as Node module and in browsers ).

( If you are reading this page on npmjs.org, I recommend you read it on Github workers-manager, because on Github it is more readable.)

Browser support

Chrome, Safari, Firefox, Opera, IE10+

Installation

    npm install workers-manager

Usage

You can use it both in Node environment and in browsers.

  • Use it as a Node module.

    var WorkersManager = require('workers-manager');

    /**
    * Imagine you have a Class called Company.
    */
    var Company = function () {
        this._init();
    };
    Company.prototype = {
        constructor: Company,

        /**
        * Assume we have an array [[0], [1,1], [1,2,3]]
        * and assume sum.js will add the numbers in each element of this array,
        * and return [0, 2, 6];
        * 
        * Note that navigator.hardwareConcurrency is not support by all browsers.
        */
        _init: function () {
            this._workerMgr = new WorkersManager();             // Initialize WorkersManager.
            this._workerMgr.setPath('test/workers/sum.js')      // Set file path.
                           .setNumOfWorkers(navigator.hardwareConcurrency || 2)     // Default is 1.
                           .events.bind('message', this._receivedMsg.bind(this))    // Bind events.
                                  .bind('error',   this._receivedErr.bind(this));   // Chainable.
        },

        getWorkerMgr: function () {
            return this._workerMgr;
        },

        /**
        * WorkersManager will return processed data all together.
        */
        _receivedMsg: function (workerMgr, msg) {
        },

        _receivedErr: function (workerMgr, msgErr) {
        }
    };

    var company = new Company(),
        mgr     = company.getWorkerMgr(),
        /**
        * Fake data
        */
        data    = [[1,2], [1,1,1], [0], [4], [1,1,1,1,1], [2,2], [-1,1], [0], [12]];  // data.length === 9

    mgr.setData(data)           // Set an array of data that you want workers to process.
       .startWork();            // Tell workers to work.

    /**
    * `message` event listener `_receivedMsg` will get noticed once all workers finished
    * that work.
    */
    _receivedMsg: function (workerMgr, msg) {
        msg.length      // 9
        msg             // [3,3,0,4,5,4,0,0,12]
    }
  • Use it directly in a browser. You can put workersManager.js in your project and include it by html script tag. ( This module depends on events.js, you need to bring events.js along with it.)

    /**
    * Before you use it in browsers, you probably will want to replace 'nameSpace' with a real 
    * namespace, and remove the use of `webworker-threads` since browsers support `Worker` natively.
    * Simply update `workersManager.js`.
    */
  • Public interface in this module. ( The code of workersManager.js is clean and well organized, I recommend you read it directly.)

    constructor: WorkersManager


    /**
     * Destroy the instance, it will `terminate` all workers.
     * @return <em>undefined</em>
     */
    destroy: function ()


    /**
    * @param path {String} -  Only accept .js file.
    * @returns {WorkersManager} A pointer to this instance, allowing call chaining.
    */
    setPath: function (path)


    hasErr: function ()


    /**
    * It will throw error if passing in an empty array.
    * 
    * @param data {Array}
    * @returns {WorkersManager} A pointer to this instance, allowing call chaining.
    */
    setData: function (data)


    /**
    * Default number of workers is 1.
    * 
    * @param num {Positive Integer}
    * @returns {WorkersManager} A pointer to this instance, allowing call chaining.
    */
    setNumOfWorkers: function (num)


    /**
    * If file path or `data` is not set, calling this method will throw error.
    * 
    * You also don't need to worry about the case that the length of `data` is 
    * smaller than the number of web workers. It will handle it well.
    * 
    * @returns {WorkersManager} A pointer to this instance, allowing call chaining.
    */
    startWork: function ()

Tests

    npm test