2.0.4 • Published 6 years ago

baby-workers v2.0.4

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

Baby workers Javascript

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
    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 workers');
}).catch(() => { // catch is not a promise
    console.log('Catch workers');
}).complete(() => {
    console.log('Complete workers');
});

Demos

Main functions

FunctionAvailableDescriptionAdditionnal
create(name: string or callback: function, callback: function) : currentWorkerROOT & NODECreate a new parent
set(data: any) : currentWorkerPARENTSet data and create a new node
map(data: array) : currentWorkerPARENTSet array and create a new node for each element in the array
push(data: any) : currentWorkerPARENTPush a new data and create a new node
error(error: string) : currentWorkerNODESet error in current worker and all parent in the tree
pop() : currentWorkerNODEStop current node
run() : PromisePARENTCreate and run nodes

Other way to run worker

FunctionAvailableDescriptionAdditionnal
stack() : PromisePARENTCreate and run nodes like a stack
next() : PromisePARENTCreate and run the next node
exec(idNode: number) : PromisePARENTCreate and run a specific node
reply(idNode: number = undefined) : PromisePARENTReply a node or all nodes if idNode is undefined
delay(time: number = 1) : PromisePARENTCreate and run nodes in a timeout
interval(time: number = 1000) : PromisePARENTCreate and run nodes in an intervalstop() : currentWorker, NODE, Stop interval

Configuration functions

FunctionAvailableDescriptionAdditionnal
cancel() : currentWorkerPARENTCancel parent worker
limit(maxWorkers: number = 0, extra: boolean = false)ALLLimit 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() : currentWorkerALLAdd virtual worker (it's like create a fake worker without callback)
removeWorker() : currentWorkerALLRemove virtual worker (it's like pop a fake worker)

Callback functions

FunctionAvailableDescriptionAdditionnal
all(...: Promise or Name of parent worker or Parent worker or Any) : PromiseROOTIt'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) : currentWorkerALLCall function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish)
then(callback: function, removeAfterCall: boolean) : currentWorkerALLCall function when current process is finish without error
catch(callback: function, removeAfterCall: boolean) : currentWorkerALLCall function when current process is finish with error

Data manager functions

FunctionAvailableDescriptionAdditionnal
save(data: any) : currentWorkerALLSave any data in current worker (node, parent or root)
_save(data: any) : currentWorkerALLSave any data in current worker (node, parent or root) from root
get() : anyALLGet data previously saved
_get() : anyALLGet data previously saved from root
flux : objectALLIT'S THE MAIN OBJECT, all data has saved here

Search functions

FunctionAvailableDescriptionAdditionnal
root() : parentWorkerNODEGet root/parent of current worker
parent(name: string, type: string = 'parent') : parentWorker OR nodeWorkerPARENT & NODEGet any parent/node going up the tree
parentNode(name: string) : parentWorker OR nodeWorkerPARENT & NODEGet any node going up the tree
node(key: number) : nodeWorkerPARENTReturn a node from parent

Get functions

FunctionAvailableDescriptionAdditionnal
getId() : numberNODEReturn id/key/index of node
getStatus() : stringALLReturn global status
getError : stringALLReturn error
getNodeStatus() : stringNODEReturn the status of node
getName() : stringALLReturn name
getType() : stringALLReturn type of current worker
getNodes() : arrayPARENTReturn all nodes
getLimit() : numberALLReturn the limit of workers allowed in current workers
getPromise() : PromiseParentReturn the promise of parent worker
getWorkers() : numberALLReturn the number of workers
getWaitingWorkers() : numberALLReturn the number of waiting workers
getRunningWorkers() : numberALLReturn the number of running workers
getTotalWorkers() : numberALLReturn the total number of workers (nodes included)
getTotalWaitingWorkers() : numberALLReturn the total number of workers (nodes included)
getTotalRunningWorkers() : numberALLReturn the total number of workers (nodes included)
2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.71

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago