baby-workers
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -14,3 +14,3 @@ const babyWorkers = require('../workers'); | ||
}, (~~(Math.random() * 1000))); | ||
}, ['a', 'b', 'c', 'd']).run().then((data) => { | ||
}).map(['a', 'b', 'c', 'd']).run().then((data) => { | ||
console.log('Unknown Promises then', data); | ||
@@ -27,3 +27,3 @@ }).catch((data) => { | ||
}, (~~(Math.random() * 1000))); | ||
}, ['e', 'f', 'g', 'h']).run(); | ||
}).map(['e', 'f', 'g', 'h']).run(); | ||
@@ -40,4 +40,4 @@ workers.basic.complete(() => { | ||
workers.create('promises', (worker, elem) => { | ||
worker.error('There are an error').pop(); // pop is important ! | ||
}, 'Test promises').run().catch((error) => { | ||
worker.error('There are an error'); | ||
}).set('Test promises').run().catch((error) => { | ||
console.log('Promises catch', '"', error, '"'); | ||
@@ -48,4 +48,4 @@ }); | ||
workers.create('promises2', (worker, elem) => { | ||
worker._save('There are no error !').pop(); // pop is important ! | ||
}, 'Test promises').run().then((data) => { | ||
worker._save('There are no error !').pop(); | ||
}).set('Test promises').run().then((data) => { | ||
console.log('Promises then', '"', data, '"'); | ||
@@ -60,3 +60,3 @@ }); | ||
}, (~~(Math.random() * 1000))); | ||
}, ['z', 'y', 'x', 'w']).stack(); // mode stack enabled | ||
}).map(['z', 'y', 'x', 'w']).stack(); // mode stack enabled | ||
workers.stack.complete(() => { | ||
@@ -72,3 +72,3 @@ console.log('All "stack" has finished'); | ||
}, (~~(Math.random() * 1000))); | ||
}, "toto").run(); | ||
}).set('toto').run(); | ||
workers.simple.complete(() => { | ||
@@ -86,6 +86,6 @@ console.log('All "simple" has finished'); | ||
// Run worker in a timeout | ||
workers.create('timeout', (worker) => { | ||
workers.create('Delay', (worker) => { | ||
console.log('Timeout called'); | ||
worker.pop(); | ||
}).timeout(1000); | ||
}).delay(1000); | ||
@@ -95,4 +95,4 @@ // push worker | ||
console.log('My elem', elem); | ||
worker.pop(); | ||
}, null).run(); | ||
worker.pop(); | ||
}).run(); | ||
workers.pushWorker.complete(() => { | ||
@@ -107,6 +107,6 @@ console.log('All "pushWorker" has finished'); | ||
worker.save(worker.get() + 1); | ||
worker._save(worker._get() + 1); | ||
worker.pop(); | ||
if (worker.get() == 5) { | ||
if (worker._get() == 5) { | ||
workers.interval.stop(); | ||
@@ -139,9 +139,14 @@ } | ||
// Cancel parent worker | ||
const runMe = workers.create('cancel', (worker) => { | ||
console.log('Here is never called'); | ||
workers.create('data2', (worker) => { | ||
console.log(worker._get()); | ||
worker.pop(); | ||
}); | ||
runMe.cancel(); | ||
}).save('elements.0.element.1.toto', []).run(); | ||
// Cancel parent worker | ||
// const runMe = workers.create('cancel', (worker) => { | ||
// console.log('Here is never called'); | ||
// worker.pop(); | ||
// }); | ||
// runMe.cancel(); | ||
// Limit workers | ||
@@ -170,3 +175,4 @@ const random = ['<r>', '<a>', '<n>', '<d>', '<o>', '<m>']; | ||
}, (~~(Math.random() * 1000))); | ||
}, random2) | ||
}) | ||
.map(random2) | ||
.limit(0) // Unlimit worker but if parent have a limit so it take parent limit | ||
@@ -181,3 +187,3 @@ .limit(-1) // Unlimit worker | ||
}, (~~(Math.random() * 1000))); | ||
}, random).limit(2).run(); | ||
}).map(random).limit(2).run(); | ||
@@ -187,3 +193,2 @@ // Set errors | ||
worker.error('Why ?', 'Because you stole my bread dude...'); | ||
worker.pop(); | ||
}).run() | ||
@@ -190,0 +195,0 @@ |
@@ -91,3 +91,3 @@ const babyWorkers = require('../workers'); | ||
worker.pop(); | ||
}, 5000); | ||
}, 1000); | ||
}).limit(2).map(['ws', 'ww', 'wf', 'wx', 'wq', 'wz', 'wa']).run(); | ||
@@ -94,0 +94,0 @@ |
@@ -31,2 +31,8 @@ const babyWorkers = require('../workers'); | ||
// Manipule worker data | ||
workers.create('data2', (worker) => { | ||
console.log(worker._get()); | ||
worker.pop(); | ||
}).save('elements.0.element.1.toto', []).run(); | ||
// All workers has finish | ||
@@ -33,0 +39,0 @@ workers.complete((error) => { |
@@ -11,6 +11,6 @@ const babyWorkers = require('../workers'); | ||
console.log('Interval called'); | ||
worker.save(worker.get() + 1); | ||
worker._save(worker._get() + 1); | ||
worker.pop(); | ||
if (worker.get() == 5) { | ||
if (worker._get() === 5) { | ||
workers.interval.stop(); | ||
@@ -17,0 +17,0 @@ } |
@@ -43,2 +43,20 @@ const babyWorkers = require('../workers'); | ||
// const random = ['<r>', '<a>', '<n>', '<d>', '<o>', '<m>', '<r>', '<a>', '<n>', '<d>', '<o>', '<m>']; | ||
// workers.create('limitWorker', (worker, randomValue) => { | ||
// console.log( | ||
// 'Id:', worker.getId(), | ||
// 'limitWorker', randomValue, | ||
// 'LimitWorker Workers:', worker.getWorkers(), | ||
// 'LimitWorker Waiting workers', worker.getWaitingWorkers(), | ||
// 'LimitWorker Running workers', worker.getRunningWorkers(), | ||
// 'Total workers:', workers.getTotalWorkers(), | ||
// 'Total waiting workers', workers.getTotalWaitingWorkers(), | ||
// 'Total running workers', workers.getTotalRunningWorkers() | ||
// ); | ||
// setTimeout(() => { | ||
// worker.pop() | ||
// }, (~~(Math.random() * 5000))); | ||
// }).map(random).limit(2).run(); | ||
// All workers has finish | ||
@@ -45,0 +63,0 @@ workers.complete((error) => { |
@@ -10,3 +10,3 @@ const babyWorkers = require('../workers'); | ||
workers.create('promise', (worker, elem) => { | ||
worker.error('There are an error').pop(); // pop is important ! | ||
worker.error('There are an error'); | ||
}).set('Test promise').run().catch((error) => { | ||
@@ -18,3 +18,3 @@ console.log('Promise catch', '"', error, '"'); | ||
workers.create('promise2', (worker, elem) => { | ||
worker._save('There are no error !').pop(); // pop is important ! | ||
worker._save('There are no error !').pop(); | ||
}).set('Test promise').run().then((data) => { | ||
@@ -21,0 +21,0 @@ console.log('Promise then', '"', data, '"'); |
@@ -23,6 +23,6 @@ const babyWorkers = require('../workers'); | ||
worker.save(worker.get() + 1); | ||
worker._save(worker._get() + 1); | ||
worker.pop(); | ||
if (worker.get() == 5) { | ||
if (worker._get() == 5) { | ||
workers.interval.stop(); | ||
@@ -29,0 +29,0 @@ } |
{ | ||
"name": "baby-workers", | ||
"version": "2.0.1", | ||
"description": "Manage your functions asynchronously or as stack with baby-workers and promise.", | ||
"version": "2.0.2", | ||
"description": "Execute and manage your code asynchronously with workers, like promise, execute each element of an array or a simple element in callback.", | ||
"main": "workers.min.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
# Workers Javascript | ||
# Baby workers Javascript | ||
Execute and manage your code asynchronously with workers, like promise, execute each element of an array or a simple element in callback. | ||
@@ -28,13 +28,13 @@ Like thread, you can start and stop your jober where and when you want in your code. You can limit the number of jobers or execute it like a stack. | ||
[Root] -> worker.create(...) -> [Parent] -> .map(['...', '...']) -> .run() -> [Node] + [Node] | ||
[Root] -> worker.create(...) -> [Parent] -> .map(['...', '...']) or .set(...) -> .run() or .stack() or [...] -> [Node] + [Node] | ||
## Usage | ||
First create a new parent => workers.create(name or function, function) | ||
Next set data or map arrary => workers.name.map([...]) | ||
Then run parent to create nodes => workers.name.run() | ||
* First create a new parent with workers.create(name or function, function) | ||
* Next set data or map array with workers.name.map([...]) or workers.name.set(...) | ||
* Then run parent to create nodes with workers.name.run() or workers.name.stack() or [...] | ||
``` js | ||
workers.create('MyWorker', (worker, elem) => { | ||
console.log('[=>', elem); | ||
console.log('|=>', elem); | ||
worker.pop(); | ||
@@ -47,4 +47,13 @@ }).map(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise | ||
worker.then(() => { // then is not a promise | ||
workers.create('MyWorker2', (worker, elem) => { | ||
console.log('|=>', elem); | ||
worker.pop(); | ||
}).set(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise | ||
console.log('Then'); | ||
}).catch(() => { // catch is a promise | ||
console.log('Catch'); | ||
}); | ||
workers.then(() => { // then is not a promise | ||
console.log('Then'); | ||
}).catch(() => { // catch is not a promise | ||
@@ -58,4 +67,2 @@ console.log('Catch'); | ||
More examples at the end of README.md file. | ||
## Demos | ||
@@ -66,3 +73,3 @@ * [Basic](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/basic.js) | ||
* [Simulate adding/removing worker](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/add_remove_worker.js) | ||
* [Set timeout](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/timeout.js) | ||
* [Set delay](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/delay.js) | ||
* [Set interval](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/interval.js) | ||
@@ -76,8 +83,18 @@ * [Push](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/push.js) | ||
## Functions | ||
## Main functions | ||
Name | Available | Description | Additionnal | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
create(name: `string` or callback: `function`, callback: `function`) : `currentWorker` | ALL | Create a new parent | ||
set(data: `any`) : `currentWorker` | PARENT | Set data and create a new node | ||
map(data: `array`) : `currentWorker` | PARENT | Set array and create a new node for each element in the array | ||
push(data: `any`) : `currentWorker` | PARENT | Push a new data and create a new node | ||
error(error: `string`) : `currentWorker` | ALL | Set error in current worker and all parent in the tree | ||
pop() : `currentWorker` | NODE | Stop current node | ||
run() : `Promise` | PARENT | Create and run nodes | ||
## Other way to run worker | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
stack() : `Promise` | PARENT | Create and run nodes like a stack | ||
@@ -89,14 +106,23 @@ next() : `Promise` | PARENT | Create and run the next node | ||
interval(time: `number = 1000`) : `Promise` | PARENT | Create and run nodes in an interval | stop() : `currentWorker`, NODE, Stop interval | ||
set(data: `any`) : `currentWorker` | PARENT | Set data it will be used to create node | ||
map(data: `array`) : `currentWorker` | PARENT | Set array it will be used to create nodes for each element in the array | ||
push(data: `any`) : `currentWorker` | PARENT | Push a new data to create a new node | ||
## Configuration functions | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
cancel() : `currentWorker` | PARENT | Cancel parent worker | ||
limit(maxWorkers: `number = 0`, extra: `boolean = false`) | ALL | Limit the number of workers as running (`maxWorkers = 0` = unlimited or take limit of parent - `maxWorkers = -1` = unlimited and ignore parent). If `extra = true` is true so maxWorkers is taken ONLY if parent workers limit is full | ||
pop() : `currentWorker` | NODE | Stop current node | ||
addWorker() : `currentWorker` | ALL | Add virtual worker in current worker (it used for external asynch function) | ||
removeWorker() : `currentWorker` | ALL | Remove virtual worker in current worker (it used for external asynch function) | ||
limit(maxWorkers: `number = 0`, extra: `boolean = false`) | ALL | Limit the number of workers (`maxWorkers = 0` = unlimited or take limit of parent - `maxWorkers = -1` = unlimited and ignore parent). If `extra = true` is true so maxWorkers is taken ONLY if parent workers limit is full | ||
addWorker() : `currentWorker` | ALL | Add virtual worker (be careful !) | ||
removeWorker() : `currentWorker` | ALL | Remove virtual worker (be careful !) | ||
## Callback functions | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
complete(callback: `function`, removeAfterCall: `boolean`) : `currentWorker` | ALL | Call function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish) | ||
then(callback: `function`, removeAfterCall: `boolean`) : `currentWorker` | ALL | Call function when current process is finish without error | ||
catch(callback: `function`, removeAfterCall: `boolean`) : `currentWorker` | ALL | Call function when current process is finish with error | ||
error(error: `string`) : `currentWorker` | ALL | Set error in current worker and all parent in the tree | ||
## Data manager functions | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
save(data: `any`) : `currentWorker` | ALL | Save any data in current worker (node, parent or root) | ||
@@ -106,2 +132,8 @@ _save(data: `any`) : `currentWorker` | ALL | Save any data in current worker (node, parent or root) from root | ||
_get() : `any` | ALL | Get data previously saved from root | ||
flux : `object` | ALL | IS NOT A FUNCTION BUT AN OBJECT ! | ||
## Search functions | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
root() : `parentWorker` | NODE | Get root/parent of current worker | ||
@@ -111,2 +143,7 @@ parent(name: `string`, type: `string = 'parent'`) : `parentWorker OR nodeWorker` | PARENT & NODE | Get any parent/node going up the tree | ||
node(key: `number`) : `nodeWorker` | PARENT | Return a node from parent | ||
## Get functions | ||
Function | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
getId() : `number` | NODE | Return id/key/index of node | ||
@@ -113,0 +150,0 @@ getStatus() : `string` | ALL | Return global status |
@@ -26,2 +26,4 @@ var Workers = function() | ||
{ | ||
this.flux = {}; | ||
this.create = function(name, callback, data) | ||
@@ -108,4 +110,4 @@ { | ||
delete _engine.this.cancel; | ||
delete _engine.this.push; | ||
delete _engine.this.cancel; | ||
delete _engine.this.node; | ||
@@ -252,3 +254,10 @@ break; | ||
{ | ||
if (_engine.status === $enum.STATUS.CANCEL) { | ||
return ; | ||
} | ||
if (_parent.stack.currentNode + 1 !== idNode && forceId === false) { | ||
_engine.this.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
return $waitingNode(idNode, forceId); | ||
@@ -263,6 +272,10 @@ } | ||
// to opti | ||
if (parent !== null && typeof(parent.back) === 'function') { | ||
if (_engine.this.getRunningWorkers() === 0 && parent !== null && typeof(parent.back) === 'function') { | ||
parent.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
} else { | ||
_engine.this.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
} | ||
@@ -278,6 +291,10 @@ // | ||
// to opti | ||
if (parent !== null && typeof(parent.back) === 'function') { | ||
if (_engine.this.getRunningWorkers() === 0 && parent !== null && typeof(parent.back) === 'function') { | ||
parent.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
} else { | ||
_engine.this.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
} | ||
@@ -290,2 +307,5 @@ // | ||
if (_parent.stack.status === true && _parent.stack.isRunning !== $enum.STATUS.WAITING) { | ||
_engine.this.back(function() { | ||
$execCallback('waiting'); | ||
}, false); | ||
return $waitingNode(idNode, forceId); | ||
@@ -364,4 +384,4 @@ } | ||
_engine.this.removeWorker(); | ||
_engine.status = $enum.STATUS.CANCEL; | ||
// _engine.this.removeWorker(); | ||
return _engine.this; | ||
@@ -448,3 +468,3 @@ } | ||
_engine.status = $enum.STATUS.FINISH; | ||
_engine.status = (_engine.status === $enum.STATUS.CANCEL ? _engine.status : $enum.STATUS.FINISH); | ||
} | ||
@@ -482,13 +502,25 @@ } | ||
//-- --// | ||
this.save = function(data) | ||
this.save = function(name, data) | ||
{ | ||
_engine.save = data; | ||
if (data === undefined) { | ||
data = name; | ||
name = undefined; | ||
} | ||
var setDataRec = function(flux, type, search) { | ||
if (search.length === 0) { | ||
flux[type] = data; | ||
return ; | ||
} | ||
flux[type] = (flux[type] === undefined ? {} : flux[type]); | ||
return setDataRec(flux[type], search.splice(0, 1).shift(), search); | ||
}; | ||
setDataRec(_engine.this, 'flux', (typeof(name) !== 'string' ? [] : name.split('.'))); | ||
return _engine.this; | ||
} | ||
this._save = function(data) | ||
this._save = function(name, data) | ||
{ | ||
var parent = _engine.parent; | ||
if (parent !== null) { | ||
parent.save(data); | ||
parent.save(name, data); | ||
} | ||
@@ -498,11 +530,18 @@ return _engine.this; | ||
this.get = function() | ||
this.get = function(name) | ||
{ | ||
return _engine.save; | ||
var getDataRec = function(flux, type, search) { | ||
if (search.length === 0) { | ||
return flux[type]; | ||
} | ||
flux[type] = (flux[type] === undefined ? {} : flux[type]); | ||
return getDataRec(flux[type], search.splice(0, 1).shift(), search); | ||
}; | ||
return getDataRec(_engine.this, 'flux', (typeof(name) !== 'string' ? [] : name.split('.'))); | ||
} | ||
this._get = function() | ||
this._get = function(name) | ||
{ | ||
var parent = _engine.parent; | ||
return (parent == null ? parent : parent.get()); | ||
return (parent === null ? null : parent.get(name)); | ||
} | ||
@@ -567,3 +606,3 @@ | ||
if (_engine.status === $enum.STATUS.FINISH && _engine.error === null) { | ||
callback(_engine.save); | ||
callback(_engine.this.flux); | ||
if (typeof(removeAfterCall) !== 'boolean' || removeAfterCall == true) { | ||
@@ -574,3 +613,3 @@ return _engine.this; | ||
$on('then', callback, removeAfterCall, function() { | ||
return _engine.save; | ||
return _engine.this.flux; | ||
}); | ||
@@ -723,3 +762,2 @@ return _engine.this; | ||
type: $enum.NONE, | ||
save: null, | ||
error: null, | ||
@@ -726,0 +764,0 @@ children: {}, |
@@ -1,1 +0,1 @@ | ||
var Workers=function(){var t=null,e={ROOT:"root",PARENT:"parent",NODE:"node"},n={CANCEL:"cancel",WAITING:"waiting",RUNNING:"running",FINISH:"finish"};Object.values="function"!=typeof Object.values?function(t){return Object.keys(t).map(function(e){return t[e]})}:Object.values;var r=function(i){this.create=function(t,n,i){return n="function"==typeof t?t:n,null===(t="function"==typeof t?null:t)||void 0===d.children[t]&&void 0===d.this[t]?(d.children[t]=new r(t),d.children[t].init(d.this,e.PARENT),d.children[t].setInfos(n),void 0!==(i="function"==typeof t?n:i)&&console.log("baby-worker : create function => data parameter is deprecated. It will be remove in v2.5.0"),!0===Array.isArray(i)?d.children[t].map(i):void 0!==i&&d.children[t].set(i),null!==t&&(d.this[t]=d.children[t]),d.children[t]):null},this.init=function(t,n,r){switch(d.id=r,d.type=n,d.parent=t,n){case e.ROOT:delete d.this.setInfos,delete d.this.set,delete d.this.map,delete d.this.timeout,delete d.this.interval,delete d.this.run,delete d.this.stack,delete d.this.push,delete d.this.cancel,delete d.this.node,delete d.this.pop,delete d.this.root,delete d.this.parent,delete d.this.back,delete d.this.getId,delete d.this.getNodeStatus;break;case e.PARENT:delete d.this.pop,delete d.this.root,delete d.this.getId,delete d.this.getNodeStatus;break;case e.NODE:delete d.this.setInfos,delete d.this.set,delete d.this.map,delete d.this.timeout,delete d.this.interval,delete d.this.run,delete d.this.stack,delete d.this.back,delete d.this.push,delete d.this.cancel,delete d.this.node}return delete d.this.init,d.this},this.setInfos=function(t){return h.callback=t,d.status=n.WAITING,delete d.this.setInfos,d.this},this.map=function(t){return!1===Array.isArray(t)?d.this.set(t):(h.data=t,d.this)},this.push=function(t){h.data.push(t);return d.type===e.PARENT&&d.status===n.FINISH&&d.this.next(),d.this},this.set=function(t){return h.data=[t],d.this},this.stack=function(){return h.stack.status=!0,d.this.next(),new Promise(function(t,e){d.this.then(t).catch(e)})},this.timeout=function(t){return console.log("baby-worker : timeout function is deprecated. It will be remove in v2.5.0"),d.this.delay(t)},this.delay=function(t){return t=null==t||"number"!=typeof t?1:t,setTimeout(d.this.run,t),new Promise(function(t,e){d.this.then(t).catch(e)})},this.interval=function(t){return t=null==t||"number"!=typeof t?1e3:t,d.this.addWorker(!0),h.isInterval=setInterval(function(){d.this.reply()},t),d.this.stop=function(){d.this.removeWorker(),clearInterval(h.isInterval)},new Promise(function(t,e){d.this.then(t).catch(e)})},this.run=function(){for(;d.this.next(););return d.this},this.reply=function(t){return void 0===t?(0===h.data.length&&h.data.push(void 0),h.data.map(function(t,e){d.this.exec(e,!0)}),d.this):d.this.exec(t,!0)},this.next=function(){return h.currentSeek+=1,null===d.this.exec(h.currentSeek,!1)?(h.currentSeek-=1,null):d.this},this.exec=function(t,n){if(n="boolean"!=typeof n||n,h.data.length<=t){if(0!==t)return null;h.data.push(void 0)}if(void 0===h.nodes[t]){var i=new r(d.name);i.init(d.this,e.NODE,t),h.nodes.push(i),h.workers+=1}return o(t,n),d.this};var o=function(t,e){if(h.stack.currentNode+1!==t&&!1===e)return a(t,e);var r=s();return h.limitWorkers>0&&h.limitWorkers<=h.runningWorkers&&(!1===h.limitExtra||null===r||r.getLimit()>0&&r.getLimit()+h.limitWorkers<=r.getTotalRunningWorkers()+d.this.getRunningWorkers())?(null!==r&&"function"==typeof r.back&&r.back(function(){c("waiting")},!1),a(t,e)):0===h.limitWorkers&&null!==r&&r.getLimit()>0&&r.getLimit()<=r.getTotalRunningWorkers()+d.this.getRunningWorkers()?(null!==r&&"function"==typeof r.back&&r.back(function(){c("waiting")},!1),a(t,e)):!0===h.stack.status&&h.stack.isRunning!==n.WAITING?a(t,e):u(t,e)},s=function(t){return void 0===t&&null!=d.parent?s(d.parent):void 0!==t&&0===t.getLimit()&&"function"==typeof t.root?s(t.root()):t},a=function(t,e){return h.waitingWorkers+=1,d.this.addWaitingWorker(),l("waiting",function(n){h.waitingWorkers-=1,d.this.removeWaitingWorker(),o(t,e)}),!1},u=function(t,e){!1===e&&(h.stack.currentNode+=1),h.runningWorkers+=1,h.stack.isRunning=n.RUNNING,h.nodes[t].addWorker(!0),h.nodes[t].complete(function(){h.runningWorkers-=1,!0===h.stack.status&&h.stack.currentNode<h.data.length?(h.stack.isRunning=n.WAITING,d.this.next()):0!==h.waitingWorkers?(h.stack.isRunning=n.WAITING,d.this.next()):null===h.isInterval&&(h.stack.isRunning=n.FINISH)});try{h.callback(h.nodes[t],h.data[t])}catch(e){h.nodes[t].error(e)}return!0};this.limit=function(t,e){return h.limitExtra="boolean"==typeof e&&e,h.limitWorkers=t<-1?-1:t,h.limitWorkers=!0===h.limitExtra&&-1===h.limitWorkers?0:h.limitWorkers,d.this},this.cancel=function(){return d.status===n.FINISH||d.status===n.CANCEL?null:(d.this.removeWorker(),d.status=n.CANCEL,d.this)},this.error=function(t,e){return e="boolean"!=typeof e||e,d.error=null===d.error?t:d.error,null!==d.parent&&d.parent.error(d.error,!1),!0===e&&d.this.pop(),d.this},this.pop=function(){return d.nodeStatus!==n.RUNNING?null:d.this.removeWorker()},this.addWorker=function(t){return t="boolean"==typeof t&&t,d.status=n.RUNNING,!1===t?d.totalRunningWorkers+=1:d.nodeStatus=n.RUNNING,null!==d.parent&&d.parent.addWorker(!1),d.this},this.removeWorker=function(t){return void 0===t&&d.type===e.NODE?(d.nodeStatus=n.FINISH,t=d.id,null!==d.error?c("catch"):c("then"),c("complete")):(d.totalRunningWorkers-=1,void 0!==t&&c("back",t),0===d.totalRunningWorkers&&0!==d.totalWaitingWorkers&&(c("waiting"),d.status=n.WAITING),0===d.totalRunningWorkers&&0===d.totalWaitingWorkers&&(null!==d.error?c("catch"):c("then"),c("complete"),d.status=n.FINISH)),null!==d.parent&&d.parent.removeWorker(t),d.this},this.addWaitingWorker=function(){return d.totalWaitingWorkers+=1,null!==d.parent&&d.parent.addWaitingWorker(),d.this},this.removeWaitingWorker=function(){return d.totalWaitingWorkers-=1,null!==d.parent&&d.parent.removeWaitingWorker(),d.this},this.save=function(t){return d.save=t,d.this},this._save=function(t){var e=d.parent;return null!==e&&e.save(t),d.this},this.get=function(){return d.save},this._get=function(){var t=d.parent;return null==t?t:t.get()},this.root=function(){return d.parent},this.parent=function(n,r){return null==d.parent?null:(r=null==r?e.PARENT:r,d.parent.getName()!==n||r!==t&&d.parent.getType()!==r?void 0===d.parent.parent?null:d.parent.parent(n,r):d.parent)},this.parentNode=function(t){return d.this.parent(t,e.NODE)},this.node=function(t){return null==h.nodes[t]?null:h.nodes[t]},this.back=function(t,e){return l("back",t,e,function(t){return t}),d.this},this.complete=function(t,e){return d.status===n.FINISH&&(t(d.error),"boolean"!=typeof e||1==e)?d.this:(l("complete",t,e,function(){return d.error}),d.this)},this.then=function(t,e){return d.status===n.FINISH&&null===d.error&&(t(d.save),"boolean"!=typeof e||1==e)?d.this:(l("then",t,e,function(){return d.save}),d.this)},this.catch=function(t,e){return d.status===n.FINISH&&null!==d.error&&(t(d.error),"boolean"!=typeof e||1==e)?d.this:(l("catch",t,e,function(){return d.error}),d.this)};var l=function(t,e,n,r){return n="boolean"!=typeof n||n,r="function"==typeof r?r:null,d.callback.push({type:t,callback:e,removeAfterCall:n,param:r}),d.this},c=function(t,e){var n=d.callback;for(var r in d.callback=[],n)n[r].type===t?("boolean"==typeof n[r].removeAfterCall&&0==n[r].removeAfterCall&&d.callback.push(n[r]),n[r].callback("function"==typeof n[r].param?n[r].param(e):n[r].param)):d.callback.push(n[r])};this.getName=function(){return d.name},this.getType=function(){return d.type},this.getId=function(){return d.id},this.getError=function(){return d.error},this.getStatus=function(){return d.status},this.getNodeStatus=function(){return d.nodeStatus},this.getLimit=function(){return h.limitWorkers},this.getWorkers=function(){return h.workers},this.getWaitingWorkers=function(){return h.waitingWorkers},this.getRunningWorkers=function(){return h.runningWorkers},this.getTotalWorkers=function(){return h.nodes.length},this.getTotalWaitingWorkers=function(){return d.totalWaitingWorkers},this.getTotalRunningWorkers=function(){return d.totalRunningWorkers},this.getNodes=function(){return h.nodes};var h={limitWorkers:0,limitExtra:!1,wasRejected:!1,isInterval:null,nodes:[],workers:0,runningWorkers:0,waitingWorkers:0,data:[],callback:null,stack:{status:!1,currentNode:-1,isRunning:n.WAITING},currentSeek:-1},d={this:this,id:null,name:i,parent:null,status:t,nodeStatus:t,type:t,save:null,error:null,children:{},callback:[],totalRunningWorkers:0,totalWaitingWorkers:0}};return new r("root").init(null,e.ROOT)};try{module.exports=Workers}catch(t){} | ||
var Workers=function(){var t=null,e={ROOT:"root",PARENT:"parent",NODE:"node"},n={CANCEL:"cancel",WAITING:"waiting",RUNNING:"running",FINISH:"finish"};Object.values="function"!=typeof Object.values?function(t){return Object.keys(t).map(function(e){return t[e]})}:Object.values;var r=function(i){this.flux={},this.create=function(t,n,i){return n="function"==typeof t?t:n,null===(t="function"==typeof t?null:t)||void 0===f.children[t]&&void 0===f.this[t]?(f.children[t]=new r(t),f.children[t].init(f.this,e.PARENT),f.children[t].setInfos(n),void 0!==(i="function"==typeof t?n:i)&&console.log("baby-worker : create function => data parameter is deprecated. It will be remove in v2.5.0"),!0===Array.isArray(i)?f.children[t].map(i):void 0!==i&&f.children[t].set(i),null!==t&&(f.this[t]=f.children[t]),f.children[t]):null},this.init=function(t,n,r){switch(f.id=r,f.type=n,f.parent=t,n){case e.ROOT:delete f.this.setInfos,delete f.this.set,delete f.this.map,delete f.this.timeout,delete f.this.interval,delete f.this.run,delete f.this.stack,delete f.this.push,delete f.this.cancel,delete f.this.node,delete f.this.pop,delete f.this.root,delete f.this.parent,delete f.this.back,delete f.this.getId,delete f.this.getNodeStatus;break;case e.PARENT:delete f.this.pop,delete f.this.root,delete f.this.getId,delete f.this.getNodeStatus;break;case e.NODE:delete f.this.setInfos,delete f.this.set,delete f.this.map,delete f.this.timeout,delete f.this.interval,delete f.this.run,delete f.this.stack,delete f.this.back,delete f.this.cancel,delete f.this.push,delete f.this.node}return delete f.this.init,f.this},this.setInfos=function(t){return h.callback=t,f.status=n.WAITING,delete f.this.setInfos,f.this},this.map=function(t){return!1===Array.isArray(t)?f.this.set(t):(h.data=t,f.this)},this.push=function(t){h.data.push(t);return f.type===e.PARENT&&f.status===n.FINISH&&f.this.next(),f.this},this.set=function(t){return h.data=[t],f.this},this.stack=function(){return h.stack.status=!0,f.this.next(),new Promise(function(t,e){f.this.then(t).catch(e)})},this.timeout=function(t){return console.log("baby-worker : timeout function is deprecated. It will be remove in v2.5.0"),f.this.delay(t)},this.delay=function(t){return t=null==t||"number"!=typeof t?1:t,setTimeout(f.this.run,t),new Promise(function(t,e){f.this.then(t).catch(e)})},this.interval=function(t){return t=null==t||"number"!=typeof t?1e3:t,f.this.addWorker(!0),h.isInterval=setInterval(function(){f.this.reply()},t),f.this.stop=function(){f.this.removeWorker(),clearInterval(h.isInterval)},new Promise(function(t,e){f.this.then(t).catch(e)})},this.run=function(){for(;f.this.next(););return f.this},this.reply=function(t){return void 0===t?(0===h.data.length&&h.data.push(void 0),h.data.map(function(t,e){f.this.exec(e,!0)}),f.this):f.this.exec(t,!0)},this.next=function(){return h.currentSeek+=1,null===f.this.exec(h.currentSeek,!1)?(h.currentSeek-=1,null):f.this},this.exec=function(t,n){if(n="boolean"!=typeof n||n,h.data.length<=t){if(0!==t)return null;h.data.push(void 0)}if(void 0===h.nodes[t]){var i=new r(f.name);i.init(f.this,e.NODE,t),h.nodes.push(i),h.workers+=1}return s(t,n),f.this};var s=function(t,e){if(f.status!==n.CANCEL){if(h.stack.currentNode+1!==t&&!1===e)return f.this.back(function(){c("waiting")},!1),u(t,e);var r=o();return h.limitWorkers>0&&h.limitWorkers<=h.runningWorkers&&(!1===h.limitExtra||null===r||r.getLimit()>0&&r.getLimit()+h.limitWorkers<=r.getTotalRunningWorkers()+f.this.getRunningWorkers())?(0===f.this.getRunningWorkers()&&null!==r&&"function"==typeof r.back?r.back(function(){c("waiting")},!1):f.this.back(function(){c("waiting")},!1),u(t,e)):0===h.limitWorkers&&null!==r&&r.getLimit()>0&&r.getLimit()<=r.getTotalRunningWorkers()+f.this.getRunningWorkers()?(0===f.this.getRunningWorkers()&&null!==r&&"function"==typeof r.back?r.back(function(){c("waiting")},!1):f.this.back(function(){c("waiting")},!1),u(t,e)):!0===h.stack.status&&h.stack.isRunning!==n.WAITING?(f.this.back(function(){c("waiting")},!1),u(t,e)):a(t,e)}},o=function(t){return void 0===t&&null!=f.parent?o(f.parent):void 0!==t&&0===t.getLimit()&&"function"==typeof t.root?o(t.root()):t},u=function(t,e){return h.waitingWorkers+=1,f.this.addWaitingWorker(),l("waiting",function(n){h.waitingWorkers-=1,f.this.removeWaitingWorker(),s(t,e)}),!1},a=function(t,e){!1===e&&(h.stack.currentNode+=1),h.runningWorkers+=1,h.stack.isRunning=n.RUNNING,h.nodes[t].addWorker(!0),h.nodes[t].complete(function(){h.runningWorkers-=1,!0===h.stack.status&&h.stack.currentNode<h.data.length?(h.stack.isRunning=n.WAITING,f.this.next()):0!==h.waitingWorkers?(h.stack.isRunning=n.WAITING,f.this.next()):null===h.isInterval&&(h.stack.isRunning=n.FINISH)});try{h.callback(h.nodes[t],h.data[t])}catch(e){h.nodes[t].error(e)}return!0};this.limit=function(t,e){return h.limitExtra="boolean"==typeof e&&e,h.limitWorkers=t<-1?-1:t,h.limitWorkers=!0===h.limitExtra&&-1===h.limitWorkers?0:h.limitWorkers,f.this},this.cancel=function(){return f.status===n.FINISH||f.status===n.CANCEL?null:(f.status=n.CANCEL,f.this)},this.error=function(t,e){return e="boolean"!=typeof e||e,f.error=null===f.error?t:f.error,null!==f.parent&&f.parent.error(f.error,!1),!0===e&&f.this.pop(),f.this},this.pop=function(){return f.nodeStatus!==n.RUNNING?null:f.this.removeWorker()},this.addWorker=function(t){return t="boolean"==typeof t&&t,f.status=n.RUNNING,!1===t?f.totalRunningWorkers+=1:f.nodeStatus=n.RUNNING,null!==f.parent&&f.parent.addWorker(!1),f.this},this.removeWorker=function(t){return void 0===t&&f.type===e.NODE?(f.nodeStatus=n.FINISH,t=f.id,null!==f.error?c("catch"):c("then"),c("complete")):(f.totalRunningWorkers-=1,void 0!==t&&c("back",t),0===f.totalRunningWorkers&&0!==f.totalWaitingWorkers&&(c("waiting"),f.status=n.WAITING),0===f.totalRunningWorkers&&0===f.totalWaitingWorkers&&(null!==f.error?c("catch"):c("then"),c("complete"),f.status=f.status===n.CANCEL?f.status:n.FINISH)),null!==f.parent&&f.parent.removeWorker(t),f.this},this.addWaitingWorker=function(){return f.totalWaitingWorkers+=1,null!==f.parent&&f.parent.addWaitingWorker(),f.this},this.removeWaitingWorker=function(){return f.totalWaitingWorkers-=1,null!==f.parent&&f.parent.removeWaitingWorker(),f.this},this.save=function(t,e){void 0===e&&(e=t,t=void 0);var n=function(t,r,i){if(0!==i.length)return t[r]=void 0===t[r]?{}:t[r],n(t[r],i.splice(0,1).shift(),i);t[r]=e};return n(f.this,"flux","string"!=typeof t?[]:t.split(".")),f.this},this._save=function(t,e){var n=f.parent;return null!==n&&n.save(t,e),f.this},this.get=function(t){var e=function(t,n,r){return 0===r.length?t[n]:(t[n]=void 0===t[n]?{}:t[n],e(t[n],r.splice(0,1).shift(),r))};return e(f.this,"flux","string"!=typeof t?[]:t.split("."))},this._get=function(t){var e=f.parent;return null===e?null:e.get(t)},this.root=function(){return f.parent},this.parent=function(n,r){return null==f.parent?null:(r=null==r?e.PARENT:r,f.parent.getName()!==n||r!==t&&f.parent.getType()!==r?void 0===f.parent.parent?null:f.parent.parent(n,r):f.parent)},this.parentNode=function(t){return f.this.parent(t,e.NODE)},this.node=function(t){return null==h.nodes[t]?null:h.nodes[t]},this.back=function(t,e){return l("back",t,e,function(t){return t}),f.this},this.complete=function(t,e){return f.status===n.FINISH&&(t(f.error),"boolean"!=typeof e||1==e)?f.this:(l("complete",t,e,function(){return f.error}),f.this)},this.then=function(t,e){return f.status===n.FINISH&&null===f.error&&(t(f.this.flux),"boolean"!=typeof e||1==e)?f.this:(l("then",t,e,function(){return f.this.flux}),f.this)},this.catch=function(t,e){return f.status===n.FINISH&&null!==f.error&&(t(f.error),"boolean"!=typeof e||1==e)?f.this:(l("catch",t,e,function(){return f.error}),f.this)};var l=function(t,e,n,r){return n="boolean"!=typeof n||n,r="function"==typeof r?r:null,f.callback.push({type:t,callback:e,removeAfterCall:n,param:r}),f.this},c=function(t,e){var n=f.callback;for(var r in f.callback=[],n)n[r].type===t?("boolean"==typeof n[r].removeAfterCall&&0==n[r].removeAfterCall&&f.callback.push(n[r]),n[r].callback("function"==typeof n[r].param?n[r].param(e):n[r].param)):f.callback.push(n[r])};this.getName=function(){return f.name},this.getType=function(){return f.type},this.getId=function(){return f.id},this.getError=function(){return f.error},this.getStatus=function(){return f.status},this.getNodeStatus=function(){return f.nodeStatus},this.getLimit=function(){return h.limitWorkers},this.getWorkers=function(){return h.workers},this.getWaitingWorkers=function(){return h.waitingWorkers},this.getRunningWorkers=function(){return h.runningWorkers},this.getTotalWorkers=function(){return h.nodes.length},this.getTotalWaitingWorkers=function(){return f.totalWaitingWorkers},this.getTotalRunningWorkers=function(){return f.totalRunningWorkers},this.getNodes=function(){return h.nodes};var h={limitWorkers:0,limitExtra:!1,wasRejected:!1,isInterval:null,nodes:[],workers:0,runningWorkers:0,waitingWorkers:0,data:[],callback:null,stack:{status:!1,currentNode:-1,isRunning:n.WAITING},currentSeek:-1},f={this:this,id:null,name:i,parent:null,status:t,nodeStatus:t,type:t,error:null,children:{},callback:[],totalRunningWorkers:0,totalWaitingWorkers:0}};return new r("root").init(null,e.ROOT)};try{module.exports=Workers}catch(t){} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59100
20
1187
155