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.
Do everything asynchronously with baby-workers !
Install
npm install --save baby-workers
How is it works ?
Three entites :
Root (instancied at the start of baby-wokers is the root level)
Parent (parent instance is created after a worker.create to create nodes)
Node (node instance created from parent is an independent function it can create a parent too).
[Root] -> worker.create(...) -> [Parent] -> .map(['...', '...']) or .set(...) -> .run() or .stack() or [...] -> [Node] + [Node]
Usage
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 [...]
workers.create('MyWorker',(worker,elem)=>{
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 MyWorker');
});
workers.create('MyWorker2',(worker,elem)=>{
setTimeout(()=>{
console.log('|=>', elem);
worker.pop();
},(~~(Math.random()*1000)));
}).set(['a','b','c','d']).run().then(()=>{// then is a promise
console.log('Then MyWorker2');
}).catch(()=>{// catch is a promise
console.log('Catch MyWorker2');
});
workers.all(workers.MyWorker,workers.MyWorker2).then(()=>{// then is a promise
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
NODE
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
next() : Promise
PARENT
Create and run the next node
exec(idNode: number) : Promise
PARENT
Create and run a specific node
reply(idNode: number = undefined) : Promise
PARENT
Reply a node or all nodes if idNode is undefined
delay(time: number = 1) : Promise
PARENT
Create and run nodes in a timeout
interval(time: number = 1000) : Promise
PARENT
Create and run nodes in an interval
stop() : currentWorker, NODE, Stop interval
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 (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
Function
Available
Description
Additionnal
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