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 = ; /** * Imagine you have a Class called Company. */ var { this; }; Companyprototype = { this_workerMgr = ; // Initialize WorkersManager. this_workerMgr // Set file path. // Default is 1. events // Bind events. ; // Chainable. } { return this_workerMgr; } /** * WorkersManager will return processed data all together. */ { } { } ; var company = mgr = company /** * Fake data */ data = 12 111 0 4 11111 22 -11 0 12; // data.length === 9 mgr // Set an array of data that you want workers to process. ; // Tell workers to work. /** * `message` event listener `_receivedMsg` will get noticed once all workers finished * that work. */ { msglength // 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.)
/** * @param path * @returns */ setPath: function (path) hasErr: function () /** * It will throw error if passing in an empty array. * * @param data * @returns */ setData: function (data) /** * Default number of workers is 1. * * @param num {Positive Integer} * @returns */ 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 */ startWork: function ()
Tests
npm test