baby-workers
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "baby-workers", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Manage your functions asynchronously or as stack with baby-workers. You can also create worker for each element of an array or object (converted to array) they will be executed from the function.", | ||
@@ -5,0 +5,0 @@ "main": "workers.min.js", |
@@ -18,3 +18,3 @@ # Workers Javascript | ||
``` | ||
create(name: string, data: object | array, callback: function, isStack: boolean) : currentWorker { | ||
create(name: string, callback: function, data: any = undefined) : currentWorker { | ||
Available: ALL, | ||
@@ -24,5 +24,26 @@ Description: Create a new worker | ||
run() : currentWorker { | ||
Available: ALL, | ||
Available: PARENT, | ||
Description: Run current worker | ||
} | ||
stack() : currentWorker { | ||
Available: PARENT, | ||
Description: Run nodes like stack | ||
} | ||
timeout(time: number = 1) : currentWorker { | ||
Available: PARENT, | ||
Description: Run nodes like stack | ||
} | ||
interval(time: number = 1000) : currentWorker { | ||
Available: PARENT, | ||
Description: Run nodes like stack | ||
Additionnal functions: | ||
stop() : currentWorker { | ||
Available: NODE, | ||
Description: Stop interval | ||
} | ||
} | ||
getStatus() : string { | ||
Available: ALL, | ||
Description: Get status of current worker | ||
} | ||
getName() : string { | ||
@@ -37,8 +58,8 @@ Available: ALL, | ||
addWorker() : currentWorker { | ||
Available: DON'T USE IT, | ||
Description: DON'T USE IT, add node in current worker (it used for external asynch function) | ||
Available: ALL, | ||
Description: Add virtual worker in current worker (it used for external asynch function) | ||
} | ||
removeWorker(isParent: boolean) : currentWorker { | ||
Available: DON'T USE IT, | ||
Description: DON'T USE IT, remove node in current worker (it used for external asynch function) | ||
Available: ALL, | ||
Description: Remove virtual worker in current worker (it used for external asynch function) | ||
} | ||
@@ -83,23 +104,54 @@ complete(callback: function) : currentWorker { | ||
const workers = new babyWorkers; | ||
workers.create('test', ['a', 'b', 'c', 'd'], (worker, id) => { | ||
workers.create('basic', (worker, id) => { | ||
setTimeout(() => { | ||
console.log('Test =>', id); | ||
console.log('basic =>', id); | ||
worker.pop(); | ||
}, (~~(Math.random() * 1000))); | ||
}).run(); | ||
workers.test.complete(() => { | ||
console.log('All "test" has finished'); | ||
}, ['a', 'b', 'c', 'd']).run(); | ||
workers.basic.complete(() => { | ||
console.log('All "basic" has finished'); | ||
}); | ||
workers.create('stack', (worker, id) => { | ||
setTimeout(() => { | ||
console.log('stack =>', id); | ||
worker.pop(); | ||
}, (~~(Math.random() * 1000))); | ||
}, ['z', 'y', 'x', 'w']).stack(); // mode stack enabled | ||
workers.stack.complete(() => { | ||
console.log('All "stack" has finished'); | ||
}); | ||
workers.create('tset', ['z', 'y', 'x', 'w'], (worker, id) => { | ||
workers.create('simple', (worker, id) => { | ||
setTimeout(() => { | ||
console.log('tset =>', id); | ||
console.log('simple =>', id); | ||
worker.pop(); | ||
}, (~~(Math.random() * 1000))); | ||
}, true).run(); // mode stack enabled | ||
workers.tset.complete(() => { | ||
console.log('All "tset" has finished'); | ||
}, "toto").run(); | ||
workers.simple.complete(() => { | ||
console.log('All "simple" has finished'); | ||
}); | ||
workers.simple.addWorker(); | ||
setTimeout(() => { | ||
console.log('Okay now "simple" is complete'); | ||
workers.simple.removeWorker(); | ||
}, 2000); | ||
workers.create('timeout', (worker) => { | ||
console.log('Timeout called'); | ||
worker.pop(); | ||
}).timeout(1000); | ||
workers.create('interval', (worker) => { | ||
console.log('Interval called'); | ||
worker.save(worker.get() + 1); | ||
worker.pop(); | ||
if (worker.get() == 5) { | ||
workers.interval.stop(); | ||
} | ||
}).interval(1000).save(0); | ||
workers.complete(() => { | ||
@@ -123,3 +175,3 @@ console.log('All "workers" has finished'); | ||
this.workers.create('users', ['58bdza054dre58a9dra56', 'ddz548ftbc5045dee87rr'], this.getUser).save([]).run(); | ||
this.workers.create('users', this.getUser, ['58bdza054dre58a9dra56', 'ddz548ftbc5045dee87rr']).save([]).run(); | ||
this.workers.users.complete((error) => { | ||
@@ -132,3 +184,3 @@ if (error !== null) { | ||
this.workers.create('articles', ['45dz54dez50dez84fzsd', 'bvc0b4b8fdfdg48dfgfd1', 'd48d4z0g4v8cx4q8sc4q'], this.getArticles).save([]).run(); | ||
this.workers.create('articles', this.getArticles, ['45dz54dez50dez84fzsd', 'bvc0b4b8fdfdg48dfgfd1', 'd48d4z0g4v8cx4q8sc4q']).save([]).run(); | ||
this.workers.articles.complete((error) => { | ||
@@ -161,4 +213,4 @@ if (error !== null) { | ||
worker.save(body); // to get idUser from getBillUser | ||
worker.create('notifyUser', [idUser], this.putNotifyUser).run(); | ||
worker.create('billUser', body.data.bills, this.getBillUser).save([]).run(); | ||
worker.create('notifyUser', this.putNotifyUser, idUser).run(); | ||
worker.create('billUser', this.getBillUser, body.data.bills).save([]).run(); | ||
worker.billUser.complete(() => { | ||
@@ -165,0 +217,0 @@ worker.root().save(worker.root().get().concat([{ |
@@ -10,2 +10,7 @@ var Workers = function() | ||
}, | ||
STATUS: { | ||
WAITING: 'waiting', | ||
RUNNING: 'running', | ||
FINISH: 'finish', | ||
}, | ||
}; | ||
@@ -15,2 +20,14 @@ | ||
{ | ||
this.create = function(name, callback, data) | ||
{ | ||
if (_engine.children[name] !== undefined || _engine.this[name] !== undefined) { | ||
return null; | ||
} | ||
_engine.children[name] = new $process(name); | ||
_engine.children[name].init(_engine.this, $enum.TYPE.PARENT); | ||
_engine.children[name].set(callback, data); | ||
_engine.this[name] = _engine.children[name]; | ||
return _engine.this[name]; | ||
} | ||
this.init = function(parent, type) | ||
@@ -23,2 +40,6 @@ { | ||
case $enum.TYPE.ROOT: | ||
delete _engine.this.timeout; | ||
delete _engine.this.interval; | ||
delete _engine.this.run; | ||
delete _engine.this.stack; | ||
delete _engine.this.pop; | ||
@@ -35,2 +56,6 @@ delete _engine.this.set; | ||
case $enum.TYPE.NODE: | ||
delete _engine.this.timeout; | ||
delete _engine.this.interval; | ||
delete _engine.this.run; | ||
delete _engine.this.stack; | ||
delete _engine.this.node; | ||
@@ -43,15 +68,14 @@ break; | ||
this.set = function(list, callback, isStack) | ||
this.set = function(callback, data) | ||
{ | ||
list = (list[0] == undefined ? [list] : Object.values(list)); | ||
for (var index in list) { | ||
data = (data == null || typeof(data) != 'object' || data[0] == undefined ? [data] : Object.values(data)); | ||
for (var index in data) { | ||
var nodeProcess = new $process(_engine.name); | ||
nodeProcess.init(_engine.this, $enum.TYPE.NODE); | ||
nodeProcess.addWorker(); | ||
_parent.nodes.push(nodeProcess); | ||
_parent.worker += 1; | ||
} | ||
_parent.stack.status = (typeof(isStack) == 'boolean' ? isStack : false); | ||
_parent.list = list; | ||
_parent.data = data; | ||
_parent.callback = callback; | ||
_engine.status = $enum.STATUS.WAITING; | ||
delete _engine.this.set; | ||
@@ -61,22 +85,41 @@ return _engine.this; | ||
this.create = function(name, list, callback, isStack) | ||
this.stack = function() | ||
{ | ||
if (_engine.children[name] !== undefined || _engine.this[name] !== undefined) { | ||
return null; | ||
} | ||
_engine.children[name] = new $process(name); | ||
_engine.children[name].init(_engine.this, $enum.TYPE.PARENT); | ||
_engine.children[name].set(list, callback, isStack); | ||
_engine.this[name] = _engine.children[name]; | ||
return _engine.this[name]; | ||
_parent.stack.status = true; | ||
_engine.this.run(); | ||
delete _engine.this.stack; | ||
return _engine.this; | ||
} | ||
this.timeout = function(time) | ||
{ | ||
time = (time == null || typeof(time) != 'number' ? 1 : time); | ||
setTimeout(_engine.this.run, time); | ||
delete _engine.this.timeout; | ||
return _engine.this; | ||
} | ||
this.interval = function(time) | ||
{ | ||
time = (time == null || typeof(time) != 'number' ? 1000 : time); | ||
_engine.this.addWorker(); | ||
var currentInterval = setInterval(_engine.this.run, time); | ||
_engine.this.stop = function() { | ||
_engine.this.removeWorker(false); | ||
clearInterval(currentInterval); | ||
}; | ||
delete _engine.this.interval; | ||
return _engine.this; | ||
} | ||
this.run = function() | ||
{ | ||
for (var index in _parent.list) { | ||
if (_parent.stack.status === true && (_parent.stack.isRunning === true || _parent.stack.currentNode !== parseInt(index))) { | ||
_engine.status = $enum.STATUS.RUNNING; | ||
for (var index in _parent.data) { | ||
if (_parent.stack.status === true && (_parent.stack.isRunning === $enum.STATUS.RUNNING || _parent.stack.currentNode !== parseInt(index))) { | ||
continue; | ||
} | ||
_parent.stack.isRunning = true; | ||
_parent.callback(_parent.nodes[index], _parent.list[index]); | ||
_parent.nodes[index].addWorker(); | ||
_parent.stack.isRunning = $enum.STATUS.RUNNING; | ||
_parent.callback(_parent.nodes[index], _parent.data[index]); | ||
} | ||
@@ -101,2 +144,7 @@ return _engine.this; | ||
this.getStatus = function() | ||
{ | ||
return _engine.status; | ||
} | ||
this.addWorker = function() | ||
@@ -115,5 +163,6 @@ { | ||
_parent.worker -= 1; | ||
_engine.status = $enum.STATUS.FINISH; | ||
if (_parent.stack.status === true) { | ||
_parent.stack.currentNode += 1; | ||
_parent.stack.isRunning = false; | ||
_parent.stack.isRunning = $enum.STATUS.WAITING; | ||
_engine.this.run(); | ||
@@ -129,3 +178,3 @@ } | ||
if (_engine.parent !== null) { | ||
_engine.parent.removeWorker(); | ||
_engine.parent.removeWorker(false); | ||
} | ||
@@ -210,3 +259,3 @@ return _engine.this; | ||
worker: 0, | ||
list: [], | ||
data: [], | ||
callback: null, | ||
@@ -216,3 +265,3 @@ stack: { | ||
currentNode: 0, | ||
isRunning: false, | ||
isRunning: $enum.STATUS.WAITING, | ||
}, | ||
@@ -225,2 +274,3 @@ }; | ||
parent: null, | ||
status: $enum.NONE, | ||
type: $enum.NONE, | ||
@@ -227,0 +277,0 @@ save: null, |
@@ -1,1 +0,1 @@ | ||
var Workers=function(){var t={NONE:null,TYPE:{ROOT:"root",PARENT:"parent",NODE:"node"}},e=function(r){this.init=function(e,r){switch(l.parent=e,l.type=r,r){case t.TYPE.ROOT:delete l.this.pop,delete l.this.set,delete l.this.parent,delete l.this.root,delete l.this.node;break;case t.TYPE.PARENT:delete l.this.pop,delete l.this.root;break;case t.TYPE.NODE:delete l.this.node}return delete l.this.init,l.this},this.set=function(r,n,a){r=void 0==r[0]?[r]:Object.values(r);for(var s in r){var i=new e(l.name);i.init(l.this,t.TYPE.NODE),i.addWorker(),o.nodes.push(i),o.worker+=1}return o.stack.status="boolean"==typeof a&&a,o.list=r,o.callback=n,delete l.this.set,l.this},this.create=function(r,n,o,a){return void 0!==l.children[r]||void 0!==l.this[r]?null:(l.children[r]=new e(r),l.children[r].init(l.this,t.TYPE.PARENT),l.children[r].set(n,o,a),l.this[r]=l.children[r],l.this[r])},this.run=function(){for(var t in o.list)(!0!==o.stack.status||!0!==o.stack.isRunning&&o.stack.currentNode===parseInt(t))&&(o.stack.isRunning=!0,o.callback(o.nodes[t],o.list[t]));return l.this},this.pop=function(){return l.totalWorkers-=1,0===l.totalWorkers&&n(),l.parent.removeWorker(!0),l.this},this.getName=function(){return l.name},this.addWorker=function(){return l.totalWorkers+=1,null!==l.parent&&l.parent.addWorker(),l.this},this.removeWorker=function(t){return"boolean"==typeof t&&!0===t&&(o.worker-=1,!0===o.stack.status&&(o.stack.currentNode+=1,o.stack.isRunning=!1,l.this.run())),l.totalWorkers-=1,0===l.totalWorkers&&n(),null!==l.parent&&l.parent.removeWorker(),l.this},this.complete=function(t,e){return 0===l.totalWorkers&&(t(l.error,l.fatalError),"boolean"!=typeof e||1==e)?l.this:(l.completeCallback.push({callback:t,removeAfterCall:e}),l.this)},this.error=function(t,e){return l.error=null===l.error?t:l.error,l.fatalError=null===l.fatalError?e:l.fatalError,null!==l.parent&&l.parent.error(t,e),l.this},this.save=function(t){return l.save=t,l.this},this.get=function(){return l.save},this.root=function(){return l.parent},this.parent=function(t){return null==l.parent?null:l.parent.getName()===t?l.parent:void 0===l.parent.parent?null:l.parent.parent(t)},this.node=function(t){return void 0==l.nodes[t]?null:l.nodes[t]};var n=function(){var t=[];for(var e in l.completeCallback)"boolean"==typeof l.completeCallback[e].removeAfterCall&&0==l.completeCallback[e]&&t.push(l.completeCallback[e]),l.completeCallback[e].callback(l.error);l.completeCallback=t},o={nodes:[],worker:0,list:[],callback:null,stack:{status:!1,currentNode:0,isRunning:!1}},l={this:this,name:r,parent:null,type:t.NONE,save:null,error:null,fatalError:null,children:{},completeCallback:[],totalWorkers: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"}},e=function(r){this.create=function(r,n,s){return void 0!==a.children[r]||void 0!==a.this[r]?null:(a.children[r]=new e(r),a.children[r].init(a.this,t.TYPE.PARENT),a.children[r].set(n,s),a.this[r]=a.children[r],a.this[r])},this.init=function(e,r){switch(a.parent=e,a.type=r,r){case t.TYPE.ROOT:delete a.this.timeout,delete a.this.interval,delete a.this.run,delete a.this.stack,delete a.this.pop,delete a.this.set,delete a.this.parent,delete a.this.root,delete a.this.node;break;case t.TYPE.PARENT:delete a.this.pop,delete a.this.root;break;case t.TYPE.NODE:delete a.this.timeout,delete a.this.interval,delete a.this.run,delete a.this.stack,delete a.this.node}return delete a.this.init,a.this},this.set=function(r,n){n=null==n||"object"!=typeof n||void 0==n[0]?[n]:Object.values(n);for(var i in n){var o=new e(a.name);o.init(a.this,t.TYPE.NODE),s.nodes.push(o),s.worker+=1}return s.data=n,s.callback=r,a.status=t.STATUS.WAITING,delete a.this.set,a.this},this.stack=function(){return s.stack.status=!0,a.this.run(),delete a.this.stack,a.this},this.timeout=function(t){return t=null==t||"number"!=typeof t?1:t,setTimeout(a.this.run,t),delete a.this.timeout,a.this},this.interval=function(t){t=null==t||"number"!=typeof t?1e3:t,a.this.addWorker();var e=setInterval(a.this.run,t);return a.this.stop=function(){a.this.removeWorker(!1),clearInterval(e)},delete a.this.interval,a.this},this.run=function(){a.status=t.STATUS.RUNNING;for(var e in s.data)(!0!==s.stack.status||s.stack.isRunning!==t.STATUS.RUNNING&&s.stack.currentNode===parseInt(e))&&(s.nodes[e].addWorker(),s.stack.isRunning=t.STATUS.RUNNING,s.callback(s.nodes[e],s.data[e]));return a.this},this.pop=function(){return a.totalWorkers-=1,0===a.totalWorkers&&n(),a.parent.removeWorker(!0),a.this},this.getName=function(){return a.name},this.getStatus=function(){return a.status},this.addWorker=function(){return a.totalWorkers+=1,null!==a.parent&&a.parent.addWorker(),a.this},this.removeWorker=function(e){return"boolean"==typeof e&&!0===e&&(s.worker-=1,a.status=t.STATUS.FINISH,!0===s.stack.status&&(s.stack.currentNode+=1,s.stack.isRunning=t.STATUS.WAITING,a.this.run())),a.totalWorkers-=1,0===a.totalWorkers&&n(),null!==a.parent&&a.parent.removeWorker(!1),a.this},this.complete=function(t,e){return 0===a.totalWorkers&&(t(a.error,a.fatalError),"boolean"!=typeof e||1==e)?a.this:(a.completeCallback.push({callback:t,removeAfterCall:e}),a.this)},this.error=function(t,e){return a.error=null===a.error?t:a.error,a.fatalError=null===a.fatalError?e:a.fatalError,null!==a.parent&&a.parent.error(t,e),a.this},this.save=function(t){return a.save=t,a.this},this.get=function(){return a.save},this.root=function(){return a.parent},this.parent=function(t){return null==a.parent?null:a.parent.getName()===t?a.parent:void 0===a.parent.parent?null:a.parent.parent(t)},this.node=function(t){return void 0==a.nodes[t]?null:a.nodes[t]};var n=function(){var t=[];for(var e in a.completeCallback)"boolean"==typeof a.completeCallback[e].removeAfterCall&&0==a.completeCallback[e]&&t.push(a.completeCallback[e]),a.completeCallback[e].callback(a.error);a.completeCallback=t},s={nodes:[],worker:0,data:[],callback:null,stack:{status:!1,currentNode:0,isRunning:t.STATUS.WAITING}},a={this:this,name:r,parent:null,status:t.NONE,type:t.NONE,save:null,error:null,fatalError:null,children:{},completeCallback:[],totalWorkers:0}};return new e("root").init(null,t.TYPE.ROOT)};module.exports=Workers; |
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
18449
252
270