# workers-manager

> Web worker manager.

Latest version **1.0.3** (published 2016-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install workers-manager
pnpm add workers-manager
yarn add workers-manager
bun add workers-manager
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2016-06-20 |
| First published | 2016-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Wenwu Wu |
| Maintainers | wenwu |
| Keywords | worker, webworker, web, manager |

## Links

- npm: https://www.npmjs.com/package/workers-manager
- Repository: https://github.com/wenwuwu/workers-manager
- Homepage: https://github.com/wenwuwu/workers-manager#readme
- Issues: https://github.com/wenwuwu/workers-manager/issues
- npm.io page: https://npm.io/package/workers-manager

## Dependencies (2)

- [events-es5](https://npm.io/package/events-es5.md) ^1.0.7
- [webworker-threads](https://npm.io/package/webworker-threads.md) ^0.7.5

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2016-06-20
- 1.0.2 — 2016-06-10
- 1.0.1 — 2016-06-08
- 1.0.0 — 2016-06-08

## README

workers-manager
=========

[![NPM version][icon-nv]][link-nv]
[![Clean Code][icon-cc]][link-cc]
[![Dependency Status][icon-ds]][link-ds]
[![devDependency Status][icon-dds]][link-dds]
![License][icon-li]

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

( If you are reading this page on [npmjs.org][link-nv], I recommend you read it 
on Github [workers-manager][link-gh-pro-home], because on Github it is more readable.)

## Browser support

Chrome, Safari, Firefox, Opera, IE10+

## Installation 

```shell
    npm install workers-manager
```

## Usage

You can use it both in Node environment and in browsers.
  
* Use it as a Node module. 

```js

    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.)

```javascript

    /**
    * 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.)

```javascript

    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

```shell
    npm test
```


[link-gh-pro-home]: https://github.com/wenwuwu/workers-manager

[icon-nv]: https://img.shields.io/npm/v/workers-manager.svg?style=flat
[link-nv]: https://www.npmjs.com/package/workers-manager
[icon-ds]: https://img.shields.io/david/wenwuwu/workers-manager.svg?style=flat
[link-ds]: https://david-dm.org/wenwuwu/workers-manager
[icon-dds]: https://img.shields.io/david/dev/wenwuwu/workers-manager.svg?style=flat
[link-dds]: https://david-dm.org/wenwuwu/workers-manager#info=devDependencies
[icon-cc]: https://img.shields.io/badge/code-clean-orange.svg?style=flat
[link-cc]: https://github.com/wenwuwu/code-convention-js
[icon-li]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat

---
_Source: https://npm.io/package/workers-manager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
