baby-workers
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -6,3 +6,6 @@ const babyWorkers = require('./workers'); | ||
const workers = new babyWorkers; | ||
// Console Time | ||
console.time('time'); | ||
// Basic worker | ||
@@ -139,2 +142,5 @@ workers.create('basic', (worker, elem) => { | ||
console.log('All "workers" has finished', 'maybe some errors ?', error, fatalError); | ||
// Console Time | ||
console.timeEnd('time'); | ||
}); |
{ | ||
"name": "baby-workers", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Manage your functions asynchronously or as stack with baby-workers.", | ||
@@ -5,0 +5,0 @@ "main": "workers.min.js", |
@@ -17,5 +17,8 @@ # Workers Javascript | ||
const babyWorkers = require('baby-workers'); | ||
const workers = new babyWorkers; | ||
// Console Time | ||
console.time('time'); | ||
// Basic worker | ||
@@ -152,2 +155,5 @@ workers.create('basic', (worker, elem) => { | ||
console.log('All "workers" has finished', 'maybe some errors ?', error, fatalError); | ||
// Console Time | ||
console.timeEnd('time'); | ||
}); | ||
@@ -176,3 +182,3 @@ ``` | ||
cancel() : `currentWorker` | PARENT | Cancel current instance and execute complete callback | ||
limit | ROOT & PARENT | Limit the number of workers as running | ||
limit(maxWorkers: `number = 0`) | ROOT & PARENT | Limit the number of workers as running (Default 0 = unlimited or take limit of parent | -1 = unlimited and ignore parent) | ||
pop() : `currentWorker` | NODE | Stop current node | ||
@@ -195,8 +201,9 @@ addWorker() : `currentWorker` | ALL | Add virtual worker in current worker (it used for external asynch function) | ||
getType() : `string` | ALL | Return type of current worker | ||
getWorkers : `number` | ALL | Return the number of workers | ||
getWaitingWorkers : `number` | ALL | Return the number of waiting workers | ||
getRunningWorkers : `number` | ALL | Return the number of running workers | ||
getTotalWorkers : `number` | ALL | Return the total number of workers (nodes included) | ||
getTotalWaitingWorkers : `number` | ALL | Return the total number of workers (nodes included) | ||
getTotalRunningWorkers : `number` | ALL | Return the total number of workers (nodes included) | ||
getLimit() : `number` | ALL | Return the limit of workers allowed in current workers | ||
getWorkers() : `number` | ALL | Return the number of workers | ||
getWaitingWorkers() : `number` | ALL | Return the number of waiting workers | ||
getRunningWorkers() : `number` | ALL | Return the number of running workers | ||
getTotalWorkers() : `number` | ALL | Return the total number of workers (nodes included) | ||
getTotalWaitingWorkers() : `number` | ALL | Return the total number of workers (nodes included) | ||
getTotalRunningWorkers() : `number` | ALL | Return the total number of workers (nodes included) | ||
@@ -203,0 +210,0 @@ ## Exemples 2 |
@@ -231,3 +231,7 @@ var Workers = function() | ||
{ | ||
return (_parent.maxWorkers == -1 ? -1 : (_parent.maxWorkers + (_engine.parent != null ? _engine.parent.getLimit() : 0))); | ||
var maxWorkersParent = (_engine.parent != null ? _engine.parent.getLimit() : 0); | ||
maxWorkersParent = (maxWorkersParent == -1 ? 0 : maxWorkersParent); | ||
var maxWorkers = (_parent.maxWorkers <= 0 ? _parent.maxWorkers : (_parent.maxWorkers + maxWorkersParent)); | ||
maxWorkers += (maxWorkersParent === 0 || (maxWorkersParent !== 0 && _parent.maxWorkers === 0) ? 0 : 1); | ||
return maxWorkers; | ||
} | ||
@@ -234,0 +238,0 @@ |
@@ -1,1 +0,1 @@ | ||
var Workers=function(){var t={NONE:null,TYPE:{ROOT:"root",PARENT:"parent",NODE:"node"},STATUS:{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 e=function(r){this.create=function(r,n,i){return void 0!==u.children[r]||void 0!==u.this[r]?null:(u.children[r]=new e(r),u.children[r].init(u.this,t.TYPE.PARENT),u.children[r].set(n,i),u.this[r]=u.children[r],u.this[r])},this.init=function(e,r,n){switch(u.parent=e,u.type=r,u.id=n,r){case t.TYPE.ROOT:delete u.this.cancel,delete u.this.getId,delete u.this.timeout,delete u.this.interval,delete u.this.run,delete u.this.stack,delete u.this.pop,delete u.this.set,delete u.this.parent,delete u.this.root,delete u.this.node;break;case t.TYPE.PARENT:delete u.this.getId,delete u.this.pop,delete u.this.root;break;case t.TYPE.NODE:delete u.this.limit,delete u.this.cancel,delete u.this.timeout,delete u.this.interval,delete u.this.run,delete u.this.stack,delete u.this.node}return delete u.this.init,u.this},this.set=function(r,n){n=null==n||0==Array.isArray(n)||void 0==n[0]?[n]:Object.values(n);for(var i in n){var a=new e(u.name);a.init(u.this,t.TYPE.NODE,i),l.nodes.push(a),l.workers+=1}return l.data=n,l.callback=r,u.status=t.STATUS.WAITING,u.this.addWorker(),delete u.this.set,u.this},this.stack=function(){return l.stack.status=!0,u.this.run(),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.timeout=function(t){return t=null==t||"number"!=typeof t?1:t,setTimeout(u.this.run,t),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.interval=function(t){return t=null==t||"number"!=typeof t?1e3:t,l.haveInterval=setInterval(u.this.run,t),u.this.stop=function(){u.this.removeWorker(!1),clearInterval(l.haveInterval)},delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.run=function(){u.status=t.STATUS.RUNNING;for(var e in l.data)n(e);return null==l.haveInterval&&u.this.removeWorker(!1),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this};var n=function(e){if(!0!==l.stack.status||l.stack.currentNode!==parseInt(e)||l.stack.isRunning!==t.STATUS.WAITING)return!0===l.stack.status?(l.waitingWorkers+=1,void u.this.waiting(i(e))):l.stack.currentNode>parseInt(e)?(l.waitingWorkers+=1,void u.this.waiting(i(e))):u.this.getLimit()>0&&u.this.getLimit()<=l.runningWorkers?(l.waitingWorkers+=1,void u.this.waiting(i(e))):void a(e);a(e,!0)},i=function(t){return function(e){n(t),l.waitingWorkers-=1,e()}},a=function(e,r){!1===r&&(l.stack.currentNode+=1),l.runningWorkers+=1,l.nodes[e].addWorker(!0),l.stack.isRunning=t.STATUS.RUNNING,l.callback(l.nodes[e],l.data[e])};this.cancel=function(){return u.this.removeWorker(!0,!0),u.this},this.pop=function(){return u.totalWorkers-=1,0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),u.parent.removeWorker(!0,!0),u.this},this.getName=function(){return u.name},this.getType=function(){return u.type},this.getId=function(){return u.id},this.getStatus=function(){return u.status},this.getLimit=function(){return-1==l.maxWorkers?-1:l.maxWorkers+(null!=u.parent?u.parent.getLimit():0)},this.getWorkers=function(){return l.workers},this.getWaitingWorkers=function(){return l.waitingWorkers},this.getRunningWorkers=function(){return l.runningWorkers},this.getTotalWorkers=function(){return u.totalWorkers},this.getTotalWaitingWorkers=function(){return u.totalWaitingWorkers},this.getTotalRunningWorkers=function(){return u.totalRunningWorkers},this.limit=function(t){return l.maxWorkers=t,u.this},this.waiting=function(t,e){return u.totalWaitingWorkers+=1,null!=u.parent?u.parent.waiting(t,e):(u.waitingCallback.push({callback:t,removeAfterCall:e}),u.this)},this.complete=function(t,e){return 0===u.totalWorkers&&(t(u.error,u.fatalError),"boolean"!=typeof e||1==e)?u.this:(u.completeCallback.push({callback:t,removeAfterCall:e}),u.this)},this.addWorker=function(t){return"boolean"==typeof t&&!0===t&&(u.totalRunningWorkers+=1),u.totalWorkers+=1,null!==u.parent&&u.parent.addWorker(t),u.this},this.removeWorker=function(e,r){return"boolean"==typeof e&&!0===e&&(l.workers-=1,l.runningWorkers-=1,u.status=t.STATUS.FINISH,!0===l.stack.status&&(l.stack.currentNode+=1,l.stack.isRunning=t.STATUS.WAITING)),"boolean"==typeof r&&!0===r&&(u.totalRunningWorkers-=1),u.totalWorkers-=1,null===u.parent&&s(),0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),null!==u.parent&&u.parent.removeWorker(!1,r),u.this},this.error=function(t,e){return u.error=null===u.error?t:u.error,u.fatalError=null===u.fatalError?e:u.fatalError,null!==u.parent&&u.parent.error(t,e),u.this},this.save=function(t){return u.save=t,u.this},this._save=function(t){var e=u.parent;return null==e?e:e.save(t)},this.get=function(){return u.save},this._get=function(){var t=u.parent;return null==t?t:t.get()},this.root=function(){return u.parent},this.parent=function(e,r){return null==u.parent?null:(r=void 0==r?t.TYPE.PARENT:r,u.parent.getName()!==e||r!==t.NONE&&u.parent.getType()!==r?void 0===u.parent.parent?null:u.parent.parent(e,r):u.parent)},this.parentNode=function(e){return u.this.parent(e,t.TYPE.NODE)},this.node=function(t){return void 0==u.nodes[t]?null:u.nodes[t]};var s=function(){var t=u.waitingCallback;u.waitingCallback=[];for(var e in t)"boolean"==typeof t[e].removeAfterCall&&0==t[e].removeAfterCall&&u.waitingCallback.push(t[e]),t[e].callback(function(){return u.totalWaitingWorkers-=1,0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),u.this})},o=function(){var t=u.completeCallback;u.completeCallback=[];for(var e in t)"boolean"==typeof t[e].removeAfterCall&&0==t[e].removeAfterCall&&u.completeCallback.push(t[e]),t[e].callback(u.error,u.fatalError)},l={maxWorkers:0,wasRejected:!1,haveInterval:null,nodes:[],workers:0,runningWorkers:0,waitingWorkers:0,data:[],callback:null,stack:{status:!1,currentNode:0,isRunning:t.STATUS.WAITING}},u={this:this,id:null,name:r,parent:null,status:t.NONE,type:t.NONE,save:null,error:null,fatalError:null,children:{},completeCallback:[],waitingCallback:[],totalWorkers:0,totalRunningWorkers:0,totalWaitingWorkers:0}};return new e("root").init(null,t.TYPE.ROOT)};module.exports=Workers; | ||
var Workers=function(){var t={NONE:null,TYPE:{ROOT:"root",PARENT:"parent",NODE:"node"},STATUS:{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 e=function(r){this.create=function(r,n,i){return void 0!==u.children[r]||void 0!==u.this[r]?null:(u.children[r]=new e(r),u.children[r].init(u.this,t.TYPE.PARENT),u.children[r].set(n,i),u.this[r]=u.children[r],u.this[r])},this.init=function(e,r,n){switch(u.parent=e,u.type=r,u.id=n,r){case t.TYPE.ROOT:delete u.this.cancel,delete u.this.getId,delete u.this.timeout,delete u.this.interval,delete u.this.run,delete u.this.stack,delete u.this.pop,delete u.this.set,delete u.this.parent,delete u.this.root,delete u.this.node;break;case t.TYPE.PARENT:delete u.this.getId,delete u.this.pop,delete u.this.root;break;case t.TYPE.NODE:delete u.this.limit,delete u.this.cancel,delete u.this.timeout,delete u.this.interval,delete u.this.run,delete u.this.stack,delete u.this.node}return delete u.this.init,u.this},this.set=function(r,n){n=null==n||0==Array.isArray(n)||void 0==n[0]?[n]:Object.values(n);for(var i in n){var a=new e(u.name);a.init(u.this,t.TYPE.NODE,i),l.nodes.push(a),l.workers+=1}return l.data=n,l.callback=r,u.status=t.STATUS.WAITING,u.this.addWorker(),delete u.this.set,u.this},this.stack=function(){return l.stack.status=!0,u.this.run(),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.timeout=function(t){return t=null==t||"number"!=typeof t?1:t,setTimeout(u.this.run,t),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.interval=function(t){return t=null==t||"number"!=typeof t?1e3:t,l.haveInterval=setInterval(u.this.run,t),u.this.stop=function(){u.this.removeWorker(!1),clearInterval(l.haveInterval)},delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this},this.run=function(){u.status=t.STATUS.RUNNING;for(var e in l.data)n(e);return null==l.haveInterval&&u.this.removeWorker(!1),delete u.this.stack,delete u.this.timeout,delete u.this.interval,u.this};var n=function(e){if(!0!==l.stack.status||l.stack.currentNode!==parseInt(e)||l.stack.isRunning!==t.STATUS.WAITING)return!0===l.stack.status?(l.waitingWorkers+=1,void u.this.waiting(i(e))):l.stack.currentNode>parseInt(e)?(l.waitingWorkers+=1,void u.this.waiting(i(e))):u.this.getLimit()>0&&u.this.getLimit()<=l.runningWorkers?(l.waitingWorkers+=1,void u.this.waiting(i(e))):void a(e);a(e,!0)},i=function(t){return function(e){n(t),l.waitingWorkers-=1,e()}},a=function(e,r){!1===r&&(l.stack.currentNode+=1),l.runningWorkers+=1,l.nodes[e].addWorker(!0),l.stack.isRunning=t.STATUS.RUNNING,l.callback(l.nodes[e],l.data[e])};this.cancel=function(){return u.this.removeWorker(!0,!0),u.this},this.pop=function(){return u.totalWorkers-=1,0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),u.parent.removeWorker(!0,!0),u.this},this.getName=function(){return u.name},this.getType=function(){return u.type},this.getId=function(){return u.id},this.getStatus=function(){return u.status},this.getLimit=function(){var t=null!=u.parent?u.parent.getLimit():0;t=-1==t?0:t;var e=l.maxWorkers<=0?l.maxWorkers:l.maxWorkers+t;return e+=0===t||0!==t&&0===l.maxWorkers?0:1},this.getWorkers=function(){return l.workers},this.getWaitingWorkers=function(){return l.waitingWorkers},this.getRunningWorkers=function(){return l.runningWorkers},this.getTotalWorkers=function(){return u.totalWorkers},this.getTotalWaitingWorkers=function(){return u.totalWaitingWorkers},this.getTotalRunningWorkers=function(){return u.totalRunningWorkers},this.limit=function(t){return l.maxWorkers=t,u.this},this.waiting=function(t,e){return u.totalWaitingWorkers+=1,null!=u.parent?u.parent.waiting(t,e):(u.waitingCallback.push({callback:t,removeAfterCall:e}),u.this)},this.complete=function(t,e){return 0===u.totalWorkers&&(t(u.error,u.fatalError),"boolean"!=typeof e||1==e)?u.this:(u.completeCallback.push({callback:t,removeAfterCall:e}),u.this)},this.addWorker=function(t){return"boolean"==typeof t&&!0===t&&(u.totalRunningWorkers+=1),u.totalWorkers+=1,null!==u.parent&&u.parent.addWorker(t),u.this},this.removeWorker=function(e,r){return"boolean"==typeof e&&!0===e&&(l.workers-=1,l.runningWorkers-=1,u.status=t.STATUS.FINISH,!0===l.stack.status&&(l.stack.currentNode+=1,l.stack.isRunning=t.STATUS.WAITING)),"boolean"==typeof r&&!0===r&&(u.totalRunningWorkers-=1),u.totalWorkers-=1,null===u.parent&&s(),0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),null!==u.parent&&u.parent.removeWorker(!1,r),u.this},this.error=function(t,e){return u.error=null===u.error?t:u.error,u.fatalError=null===u.fatalError?e:u.fatalError,null!==u.parent&&u.parent.error(t,e),u.this},this.save=function(t){return u.save=t,u.this},this._save=function(t){var e=u.parent;return null==e?e:e.save(t)},this.get=function(){return u.save},this._get=function(){var t=u.parent;return null==t?t:t.get()},this.root=function(){return u.parent},this.parent=function(e,r){return null==u.parent?null:(r=void 0==r?t.TYPE.PARENT:r,u.parent.getName()!==e||r!==t.NONE&&u.parent.getType()!==r?void 0===u.parent.parent?null:u.parent.parent(e,r):u.parent)},this.parentNode=function(e){return u.this.parent(e,t.TYPE.NODE)},this.node=function(t){return void 0==u.nodes[t]?null:u.nodes[t]};var s=function(){var t=u.waitingCallback;u.waitingCallback=[];for(var e in t)"boolean"==typeof t[e].removeAfterCall&&0==t[e].removeAfterCall&&u.waitingCallback.push(t[e]),t[e].callback(function(){return u.totalWaitingWorkers-=1,0===u.totalWorkers&&0===u.totalWaitingWorkers&&o(),u.this})},o=function(){var t=u.completeCallback;u.completeCallback=[];for(var e in t)"boolean"==typeof t[e].removeAfterCall&&0==t[e].removeAfterCall&&u.completeCallback.push(t[e]),t[e].callback(u.error,u.fatalError)},l={maxWorkers:0,wasRejected:!1,haveInterval:null,nodes:[],workers:0,runningWorkers:0,waitingWorkers:0,data:[],callback:null,stack:{status:!1,currentNode:0,isRunning:t.STATUS.WAITING}},u={this:this,id:null,name:r,parent:null,status:t.NONE,type:t.NONE,save:null,error:null,fatalError:null,children:{},completeCallback:[],waitingCallback:[],totalWorkers:0,totalRunningWorkers:0,totalWaitingWorkers:0}};return new e("root").init(null,t.TYPE.ROOT)};module.exports=Workers; |
35145
545
317