baby-workers
Advanced tools
Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "baby-workers", | ||
"version": "0.0.5", | ||
"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.", | ||
"version": "0.0.6", | ||
"description": "Manage your functions asynchronously or as stack with baby-workers.", | ||
"main": "workers.min.js", | ||
@@ -14,7 +14,8 @@ "scripts": { | ||
"keywords": [ | ||
"baby-workers", | ||
"baby", | ||
"javascript", | ||
"nodejs", | ||
"workers", | ||
"worker", | ||
"error", | ||
"manager", | ||
"asynchronous", | ||
@@ -29,8 +30,7 @@ "function", | ||
"get", | ||
"timeout", | ||
"interval", | ||
"callback", | ||
"js", | ||
"manage", | ||
"timeout", | ||
"interval", | ||
"setTimeout", | ||
"setInterval", | ||
"synchronous", | ||
@@ -37,0 +37,0 @@ "asynch", |
184
README.md
# Workers Javascript | ||
Manage your functions asynchronously or as stack/timeout/interval with baby-workers. | ||
You can also create worker for a simple function or for each element of an array or any data they will be executed like worker in callback. | ||
## How install it ? | ||
## Install | ||
``` | ||
``` bash | ||
npm install --save baby-workers | ||
@@ -12,95 +11,6 @@ | ||
## How is it works ? | ||
## Usage | ||
Create as much workers that you need, for a simple function or for each element of an array, they will be executed in specific callback ! | ||
Three entites : | ||
* Root (default instance never use by you) | ||
* Parent (parent instance created by worker.create) | ||
* Node (node instance created by parent for each element of array). | ||
The principe is to run asynchronouse (or not) function easily and manage them when the worker has finished with a callback. | ||
Launch any request on any element. | ||
## Functions ? | ||
``` | ||
create(name: string, callback: function, data: any = undefined) : currentWorker { | ||
Available: ALL, | ||
Description: Create a new worker | ||
} | ||
run() : currentWorker { | ||
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 run in setTimeout | ||
} | ||
interval(time: number = 1000) : currentWorker { | ||
Available: PARENT, | ||
Description: Run nodes like run in setInterval | ||
Additionnal functions: | ||
stop() : currentWorker { | ||
Available: NODE, | ||
Description: Stop interval | ||
} | ||
} | ||
getStatus() : string { | ||
Available: ALL, | ||
Description: Get status of current worker | ||
} | ||
getName() : string { | ||
Available: ALL, | ||
Description: Get name of current worker | ||
} | ||
pop() : currentWorker { | ||
Available: NODE, | ||
Description: Stop current node | ||
} | ||
addWorker() : currentWorker { | ||
Available: ALL, | ||
Description: Add virtual worker in current worker (it used for external asynch function) | ||
} | ||
removeWorker(isParent: boolean) : currentWorker { | ||
Available: ALL, | ||
Description: Remove virtual worker in current worker (it used for external asynch function) | ||
} | ||
complete(callback: function) : currentWorker { | ||
Available: ALL, | ||
Description: Call function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish) | ||
} | ||
error(error: string, fatalError: string) : currentWorker { | ||
Available: ALL, | ||
Description: Set error in current worker and all parent in the tree | ||
} | ||
save(data: any) : currentWorker { | ||
Available: ALL, | ||
Description: Save any data in current worker (node, parent or root) | ||
} | ||
get() : any { | ||
Available: ALL, | ||
Description: Get data previously saved | ||
} | ||
root() : parentWorker { | ||
Available: NODE, | ||
Description: Get root/parent of current worker | ||
} | ||
parent(name: string) : parentWorker | nodeWorker { | ||
Available: PARENT & NODE, | ||
Description: Get any parent/node going up the tree | ||
} | ||
node(key: number) : nodeWorker { | ||
Available: PARENT, | ||
Description: Get direct node going down the tree | ||
} | ||
``` | ||
## Exemples : | ||
``` javascript | ||
``` js | ||
const babyWorkers = require('baby-workers'); | ||
@@ -110,2 +20,3 @@ | ||
// Basic worker | ||
workers.create('basic', (worker, id) => { | ||
@@ -120,3 +31,4 @@ setTimeout(() => { | ||
}); | ||
// Stack worker | ||
workers.create('stack', (worker, id) => { | ||
@@ -132,2 +44,3 @@ setTimeout(() => { | ||
// Basic worker without array | ||
workers.create('simple', (worker, id) => { | ||
@@ -143,2 +56,3 @@ setTimeout(() => { | ||
// Simulate adding a worker | ||
workers.simple.addWorker(); | ||
@@ -149,3 +63,4 @@ setTimeout(() => { | ||
}, 2000); | ||
// Run worker in a timeout | ||
workers.create('timeout', (worker) => { | ||
@@ -156,2 +71,3 @@ console.log('Timeout called'); | ||
// Run worker in a setInterval | ||
workers.create('interval', (worker) => { | ||
@@ -167,3 +83,30 @@ console.log('Interval called'); | ||
workers.complete(() => { | ||
// Manipule worker data | ||
workers.create('data', (worker) => { | ||
var tab = worker.root().get(); | ||
tab.push('coucou'); | ||
worker.root().save(tab); | ||
worker.save('coucou is my name'); | ||
worker.create('data2', (worker) => { | ||
var tab = worker.parent('data').get(); | ||
tab.push(worker.parentNode('data').get()); | ||
worker.parent('data').save(tab); | ||
worker.pop(); | ||
}).run(); | ||
worker.complete(() => { | ||
console.log('Tab ?', worker.root().get()); | ||
}); | ||
worker.pop(); | ||
}).save([]).run(); | ||
// Set errors | ||
workers.create('error', (worker) => { | ||
worker.error('Why ?', 'Because you stole my bread dude...') | ||
worker.pop(); | ||
}); | ||
// All workers has finish | ||
workers.complete((error, fatalError) => { | ||
console.log('All "workers" has finished'); | ||
@@ -173,6 +116,39 @@ }); | ||
## How is it works ? | ||
## Exemples 2: | ||
Three entites : | ||
* Root (default instance never use by you) | ||
* Parent (parent instance created by worker.create) | ||
* Node (node instance created by parent for each element of array). | ||
``` javascript | ||
The principe is to run asynchronouse (or not) function easily and manage them when the worker has finished with a callback. | ||
Launch any request on any element. | ||
## Functions | ||
Name | Available | Description | Additionnal | ||
---- | --------- | ----------- | ----------- | ||
create(name: `string`, callback: `function`, data: `any = undefined`) : `currentWorker` | ALL | Create a new worker | ||
run() : `currentWorker` | PARENT | Run current worker | ||
stack() : `currentWorker` | PARENT | Run nodes like stack | ||
timeout(time: `number = 1`) : `currentWorker` | PARENT | Run nodes like run in setTimeout | ||
interval(time: `number = 1000`) : `currentWorker` | PARENT | Run nodes like run in setInterval | stop() : `currentWorker`, NODE, Stop interval | ||
getStatus() : `string` | ALL | Get status of current worker | ||
getName() : `string` | ALL | Get name of current worker | ||
getType() : `string` | ALL | Return type of current worker | ||
pop() : `currentWorker` | NODE | Stop current node | ||
addWorker() : `currentWorker` | ALL | Add virtual worker in current worker (it used for external asynch function) | ||
removeWorker(isParent: `boolean`) : `currentWorker` | ALL | Remove virtual worker in current worker (it used for external asynch function) | ||
complete(callback: `function`) : `currentWorker` | ALL | Call function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish) | ||
error(error: `string`, fatalError: `string`) : `currentWorker` | ALL | Set error in current worker and all parent in the tree | ||
save(data: `any`) : `currentWorker` | ALL | Save any data in current worker (node, parent or root) | ||
get() : `any` | ALL | Get data previously saved | ||
root() : `parentWorker` | NODE | Get root/parent of current worker | ||
parent(name: `string`, type: `string = 'parent'`) : `parentWorker OR nodeWorker` | PARENT & NODE | Get any parent/node going up the tree | ||
parentNode(name: `string`) : `parentWorker OR nodeWorker` | PARENT & NODE | Get any node going up the tree | ||
node(key: `number`) : `nodeWorker` | PARENT | Get direct node going down the tree | ||
## Exemples 2 | ||
``` js | ||
const request = require('request'); | ||
@@ -227,5 +203,5 @@ const babyWorkers = require('baby-workers'); | ||
worker.root().save(worker.root().get().concat([{ | ||
firstName: body.data.firstName, | ||
lastName: body.data.lastName, | ||
bills: worker.billUser.get(), | ||
firstName: body.data.firstName, | ||
lastName: body.data.lastName, | ||
bills: worker.billUser.get(), | ||
}])); | ||
@@ -259,3 +235,3 @@ worker.pop(); | ||
worker.root().save(worker.root().get().concat([{ | ||
name: body.data.name, | ||
name: body.data.name, | ||
amount: body.data.amount, | ||
@@ -262,0 +238,0 @@ }])); |
@@ -76,2 +76,3 @@ var Workers = function() | ||
_engine.status = $enum.STATUS.WAITING; | ||
_engine.totalWorkers = 1; | ||
delete _engine.this.set; | ||
@@ -121,2 +122,5 @@ return _engine.this; | ||
} | ||
if (_parent.stack.currentNode == 0) { | ||
_engine.totalWorkers -= 1; | ||
} | ||
return _engine.this; | ||
@@ -140,2 +144,7 @@ } | ||
this.getType = function() | ||
{ | ||
return _engine.type; | ||
} | ||
this.getStatus = function() | ||
@@ -219,3 +228,3 @@ { | ||
this.parent = function(name) | ||
this.parent = function(name, type) | ||
{ | ||
@@ -225,3 +234,4 @@ if (_engine.parent == null) { | ||
} | ||
if (_engine.parent.getName() === name) { | ||
type = (type == undefined ? $enum.TYPE.PARENT : type); | ||
if (_engine.parent.getName() === name && (type === $enum.NONE || _engine.parent.getType() === type)) { | ||
return _engine.parent; | ||
@@ -232,5 +242,10 @@ } | ||
} | ||
return _engine.parent.parent(name); | ||
return _engine.parent.parent(name, type); | ||
} | ||
this.parentNode = function(name) | ||
{ | ||
return _engine.this.parent(name, $enum.TYPE.NODE); | ||
} | ||
this.node = function(key) | ||
@@ -237,0 +252,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"}},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; | ||
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,a.totalWorkers=1,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 0==s.stack.currentNode&&(a.totalWorkers-=1),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.getType=function(){return a.type},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(e,r){return null==a.parent?null:(r=void 0==r?t.TYPE.PARENT:r,a.parent.getName()!==e||r!==t.NONE&&a.parent.getType()!==r?void 0===a.parent.parent?null:a.parent.parent(e,r):a.parent)},this.parentNode=function(e){return a.this.parent(e,t.TYPE.NODE)},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
22015
5
342
253