README
workers-manager
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 htmlscript
tag. ( This module depends onevents.js
, you need to bringevents.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