Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baby-workers

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baby-workers - npm Package Compare versions

Comparing version 2.0.3 to 2.0.4

examples/compact.js

4

package.json
{
"name": "baby-workers",
"version": "2.0.3",
"description": "Execute and manage your code asynchronously with workers, like promise, execute each element of an array or a simple element in callback.",
"version": "2.0.4",
"description": "Execute and manage your code asynchronously with workers and promise.",
"main": "workers.min.js",

@@ -6,0 +6,0 @@ "scripts": {

# 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.
Execute and manage your code asynchronously with workers and promise. Execute each element of an array or a simple element in callback.
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.

@@ -13,10 +13,2 @@ Do everything asynchronously with baby-workers !

## Last release
Version | Change log | Compatibility
---- | --------- | -----------
2.0.0 | Add map, set, push (data), next, exec, reply and getError functions + fix some bugs | >= 2.0.0
2.0.0 beta | Add promise after run/timeout/interval/stack function + add then/catch callback + catch error in run/timeout/interval/stack callback + add feature to create worker without name | >= 2.0.0
1.0.71 | Add getNodes function | <= 1.0.71
## How is it works ?

@@ -37,27 +29,37 @@

``` js
``` javascript
workers.create('MyWorker', (worker, elem) => {
console.log('|=>', elem);
worker.pop();
}).map(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise
console.log('Then');
setTimeout(() => {
console.log('|=>', elem);
worker.pop();
}, (~~(Math.random() * 1000)));
}).map(['a', 'b', 'c', 'd']).limit(2).run().then(() => { // then is a promise
console.log('Then MyWorker');
}).catch(() => { // catch is a promise
console.log('Catch');
console.log('Catch MyWorker');
});
workers.create('MyWorker2', (worker, elem) => {
console.log('|=>', elem);
worker.pop();
setTimeout(() => {
console.log('|=>', elem);
worker.pop();
}, (~~(Math.random() * 1000)));
}).set(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise
console.log('Then');
console.log('Then MyWorker2');
}).catch(() => { // catch is a promise
console.log('Catch');
console.log('Catch MyWorker2');
});
workers.all(workers.MyWorker, workers.MyWorker2).then(() => { // then is a promise
console.log('Then workers all');
}).catch(() => { // catch is a promise
console.log('Catch workers all');
});
workers.then(() => { // then is not a promise
console.log('Then');
console.log('Then workers');
}).catch(() => { // catch is not a promise
console.log('Catch');
console.log('Catch workers');
}).complete(() => {
console.log('Complete');
console.log('Complete workers');
});

@@ -68,14 +70,14 @@

## Demos
* [Basic](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/basic.js)
* [Promise](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/promise.js)
* [Stack](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/stack.js)
* [Simulate adding/removing worker](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/add_remove_worker.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)
* [Push](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/push.js)
* [Adding data](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/data.js)
* [Cancel worker](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/cancel.js)
* [Limit workers with a queue](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/limit.js)
* [Set error](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/error.js)
* [All](https://raw.githubusercontent.com/dobobaie/baby-workers/master/examples/all.js)
* [Basic](https://github.com/dobobaie/baby-workers/wiki/Basic)
* [Promise](https://github.com/dobobaie/baby-workers/wiki/Promise)
* [Stack](https://github.com/dobobaie/baby-workers/wiki/Stack)
* [Simulate adding/removing worker](https://github.com/dobobaie/baby-workers/wiki/RemoveWorker-AddWorker)
* [Set delay](https://github.com/dobobaie/baby-workers/wiki/Delay)
* [Set interval](https://github.com/dobobaie/baby-workers/wiki/Interval)
* [Push](https://github.com/dobobaie/baby-workers/wiki/Push)
* [Adding data](https://github.com/dobobaie/baby-workers/wiki/Data)
* [Cancel worker](https://github.com/dobobaie/baby-workers/wiki/Cancel)
* [Limit workers with a queue](https://github.com/dobobaie/baby-workers/wiki/Limit)
* [Set error](https://github.com/dobobaie/baby-workers/wiki/Error)
* [All](https://github.com/dobobaie/baby-workers/wiki/All)

@@ -86,7 +88,7 @@ ## Main functions

---- | --------- | ----------- | -----------
create(name: `string` or callback: `function`, callback: `function`) : `currentWorker` | ALL | Create a new parent
create(name: `string` or callback: `function`, callback: `function`) : `currentWorker` | ROOT & NODE | 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
error(error: `string`) : `currentWorker` | NODE | Set error in current worker and all parent in the tree
pop() : `currentWorker` | NODE | Stop current node

@@ -111,5 +113,6 @@ run() : `Promise` | PARENT | Create and run nodes

cancel() : `currentWorker` | PARENT | Cancel parent worker
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 !)
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 limit). If `extra = true` add extra limit ONLY IF PARENT WORKER IS FULL
addWorker() : `currentWorker` | ALL | Add virtual worker (it's like create a fake worker without callback)
removeWorker() : `currentWorker` | ALL | Remove virtual worker (it's like pop a fake worker)
## Callback functions

@@ -119,2 +122,3 @@

---- | --------- | ----------- | -----------
all(...: `Promise` or `Name of parent worker` or `Parent worker` or `Any`) : `Promise` | ROOT | It's like `all` function in Promise, the difference is this function add a virtual worker and remove it after all elements has finished
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)

@@ -132,3 +136,3 @@ then(callback: `function`, removeAfterCall: `boolean`) : `currentWorker` | ALL | Call function when current process is finish without error

_get() : `any` | ALL | Get data previously saved from root
flux : `object` | ALL | IS NOT A FUNCTION BUT AN OBJECT !
flux : `object` | ALL | IT'S THE MAIN OBJECT, all data has saved here

@@ -156,2 +160,3 @@ ## Search functions

getLimit() : `number` | ALL | Return the limit of workers allowed in current workers
getPromise() : `Promise` | Parent | Return the promise of parent worker
getWorkers() : `number` | ALL | Return the number of workers

@@ -158,0 +163,0 @@ getWaitingWorkers() : `number` | ALL | Return the number of waiting workers

@@ -89,4 +89,6 @@ var Workers = function()

delete _engine.this.getNodeStatus;
delete _engine.this.getPromise;
break;
case $enum.TYPE.PARENT:
delete _engine.this.all;
delete _engine.this.pop;

@@ -100,2 +102,3 @@ delete _engine.this.root;

delete _engine.this.setInfos;
delete _engine.this.all;
delete _engine.this.set;

@@ -114,2 +117,4 @@ delete _engine.this.map;

delete _engine.this.node;
delete _engine.this.getPromise;
break;

@@ -129,2 +134,40 @@ }

this.all = function()
{
_engine.this.addWorker();
var args = Object.values(arguments);
var dataError = null;
var dataReturned = [];
var totalRunner = args.length;
return new Promise(function(resolve, reject) {
var updateRunner = function() {
totalRunner -= 1;
if (totalRunner === 0) {
if (dataError !== null) {
reject(dataError);
} else {
resolve(dataReturned);
}
_engine.this.removeWorker();
}
}
args.map(function(elem, index) {
if (typeof(elem.then) === 'function' && typeof(elem.catch) === 'function') {
elem.then(function(data) {
dataReturned[index] = data;
updateRunner();
}).catch(function(error) {
dataError = error;
updateRunner();
});
} else {
updateRunner();
}
});
});
}
//-- --//

@@ -161,6 +204,3 @@ this.map = function(arr)

_engine.this.next();
return new Promise(function(resolve, reject) {
_engine.this.then(resolve).catch(reject);
});
return $createPromise();
}

@@ -180,6 +220,3 @@

setTimeout(_engine.this.run, time);
return new Promise(function(resolve, reject) {
_engine.this.then(resolve).catch(reject);
});
return $createPromise();
}

@@ -198,6 +235,3 @@

};
return new Promise(function(resolve, reject) {
_engine.this.then(resolve).catch(reject);
});
return $createPromise();
}

@@ -208,3 +242,3 @@

while (_engine.this.next());
return _engine.this;
return $createPromise();
}

@@ -221,3 +255,3 @@

});
return _engine.this;
return $createPromise();
}

@@ -234,3 +268,3 @@ return _engine.this.exec(idNode, true);

}
return _engine.this;
return $createPromise();
}

@@ -257,5 +291,13 @@

$verifyNodeStatus(idNode, forceId);
return _engine.this;
return $createPromise();
}
var $createPromise = function()
{
_engine.promise = new Promise(function(resolve, reject) {
_engine.this.then(resolve).catch(reject);
});
return _engine.promise;
}
var $verifyNodeStatus = function(idNode, forceId)

@@ -693,2 +735,7 @@ {

this.getPromise = function()
{
return _engine.promise;
}
this.getTotalRunningWorkers = function()

@@ -732,2 +779,3 @@ {

type: $enum.NONE,
promise: null,
error: null,

@@ -734,0 +782,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.flux={},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.cancel,delete d.this.push,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 s(t,n),d.this};var s=function(t,e){if(d.status!==n.CANCEL){if(h.stack.currentNode+1!==t&&!1===e)return 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()+d.this.getRunningWorkers())?u(t,e):0===h.limitWorkers&&null!==r&&r.getLimit()>0&&r.getLimit()<=r.getTotalRunningWorkers()+d.this.getRunningWorkers()?u(t,e):!0===h.stack.status&&h.stack.isRunning!==n.WAITING?u(t,e):a(t,e)}},o=function(t){return void 0===t&&null!=d.parent?o(d.parent):void 0!==t&&0===t.getLimit()&&"function"==typeof t.root?o(t.root()):t},u=function(t,e){return h.waitingWorkers+=1,d.this.addWaitingWorker(),l("waiting",function(n){h.waitingWorkers-=1,d.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,d.this.next()):0!==h.waitingWorkers?(h.stack.isRunning=n.WAITING,d.this.next()):null===h.isInterval&&(h.stack.isRunning=n.FINISH),c("waiting")});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.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=d.status===n.CANCEL?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,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(d.this,"flux","string"!=typeof t?[]:t.split(".")),d.this},this._save=function(t,e){var n=d.parent;return null!==n&&n.save(t,e),d.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(d.this,"flux","string"!=typeof t?[]:t.split("."))},this._get=function(t){var e=d.parent;return null===e?null:e.get(t)},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.this.flux),"boolean"!=typeof e||1==e)?d.this:(l("then",t,e,function(){return d.this.flux}),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,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,delete f.this.getPromise;break;case e.PARENT:delete f.this.all,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.all,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,delete f.this.getPromise}return delete f.this.init,f.this},this.setInfos=function(t){return d.callback=t,f.status=n.WAITING,delete f.this.setInfos,f.this},this.all=function(){f.this.addWorker();var t=Object.values(arguments),e=null,n=[],r=t.length;return new Promise(function(i,s){var o=function(){0===(r-=1)&&(null!==e?s(e):i(n),f.this.removeWorker())};t.map(function(t,r){"function"==typeof t.then&&"function"==typeof t.catch?t.then(function(t){n[r]=t,o()}).catch(function(t){e=t,o()}):o()})})},this.map=function(t){return!1===Array.isArray(t)?f.this.set(t):(d.data=t,f.this)},this.push=function(t){d.data.push(t);return f.type===e.PARENT&&f.status===n.FINISH&&f.this.next(),f.this},this.set=function(t){return d.data=[t],f.this},this.stack=function(){return d.stack.status=!0,f.this.next(),s()},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),s()},this.interval=function(t){return t=null==t||"number"!=typeof t?1e3:t,f.this.addWorker(!0),d.isInterval=setInterval(function(){f.this.reply()},t),f.this.stop=function(){f.this.removeWorker(),clearInterval(d.isInterval)},s()},this.run=function(){for(;f.this.next(););return s()},this.reply=function(t){return void 0===t?(0===d.data.length&&d.data.push(void 0),d.data.map(function(t,e){f.this.exec(e,!0)}),s()):f.this.exec(t,!0)},this.next=function(){return d.currentSeek+=1,null===f.this.exec(d.currentSeek,!1)?(d.currentSeek-=1,null):s()},this.exec=function(t,n){if(n="boolean"!=typeof n||n,d.data.length<=t){if(0!==t)return null;d.data.push(void 0)}if(void 0===d.nodes[t]){var i=new r(f.name);i.init(f.this,e.NODE,t),d.nodes.push(i),d.workers+=1}return o(t,n),s()};var s=function(){return f.promise=new Promise(function(t,e){f.this.then(t).catch(e)}),f.promise},o=function(t,e){if(f.status!==n.CANCEL){if(d.stack.currentNode+1!==t&&!1===e)return a(t,e);var r=u();return d.limitWorkers>0&&d.limitWorkers<=d.runningWorkers&&(!1===d.limitExtra||null===r||r.getLimit()>0&&r.getLimit()+d.limitWorkers<=r.getTotalRunningWorkers()+f.this.getRunningWorkers())?a(t,e):0===d.limitWorkers&&null!==r&&r.getLimit()>0&&r.getLimit()<=r.getTotalRunningWorkers()+f.this.getRunningWorkers()?a(t,e):!0===d.stack.status&&d.stack.isRunning!==n.WAITING?a(t,e):l(t,e)}},u=function(t){return void 0===t&&null!=f.parent?u(f.parent):void 0!==t&&0===t.getLimit()&&"function"==typeof t.root?u(t.root()):t},a=function(t,e){return d.waitingWorkers+=1,f.this.addWaitingWorker(),c("waiting",function(n){d.waitingWorkers-=1,f.this.removeWaitingWorker(),o(t,e)}),!1},l=function(t,e){!1===e&&(d.stack.currentNode+=1),d.runningWorkers+=1,d.stack.isRunning=n.RUNNING,d.nodes[t].addWorker(!0),d.nodes[t].complete(function(){d.runningWorkers-=1,!0===d.stack.status&&d.stack.currentNode<d.data.length?(d.stack.isRunning=n.WAITING,f.this.next()):0!==d.waitingWorkers?(d.stack.isRunning=n.WAITING,f.this.next()):null===d.isInterval&&(d.stack.isRunning=n.FINISH),h("waiting")});try{d.callback(d.nodes[t],d.data[t])}catch(e){d.nodes[t].error(e)}return!0};this.limit=function(t,e){return d.limitExtra="boolean"==typeof e&&e,d.limitWorkers=t<-1?-1:t,d.limitWorkers=!0===d.limitExtra&&-1===d.limitWorkers?0:d.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?h("catch"):h("then"),h("complete")):(f.totalRunningWorkers-=1,void 0!==t&&h("back",t),0===f.totalRunningWorkers&&0!==f.totalWaitingWorkers&&(h("waiting"),f.status=n.WAITING),0===f.totalRunningWorkers&&0===f.totalWaitingWorkers&&(null!==f.error?h("catch"):h("then"),h("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==d.nodes[t]?null:d.nodes[t]},this.back=function(t,e){return c("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:(c("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:(c("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:(c("catch",t,e,function(){return f.error}),f.this)};var c=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},h=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 d.limitWorkers},this.getWorkers=function(){return d.workers},this.getWaitingWorkers=function(){return d.waitingWorkers},this.getRunningWorkers=function(){return d.runningWorkers},this.getTotalWorkers=function(){return d.nodes.length},this.getTotalWaitingWorkers=function(){return f.totalWaitingWorkers},this.getPromise=function(){return f.promise},this.getTotalRunningWorkers=function(){return f.totalRunningWorkers},this.getNodes=function(){return d.nodes};var d={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,promise:null,error:null,children:{},callback:[],totalRunningWorkers:0,totalWaitingWorkers:0}};return new r("root").init(null,e.ROOT)};try{module.exports=Workers}catch(t){}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc