Workers Javascript
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.
How is it works ?
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, data: object | array, callback: function, isStack: boolean) : currentWorker {
Available: ALL,
Description: Create a new worker
}
run() : currentWorker {
Available: ALL,
Description: Run current worker
}
getName() : string {
Available: ALL,
Description: Get name of current worker
}
pop() : currentWorker {
Available: NODE,
Description: Stop current node
}
addWorker() : currentWorker {
Available: DON'T USE IT,
Description: DON'T USE IT, add node 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)
}
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 :
const babyWorkers = require('baby-workers');
const workers = new babyWorkers;
workers.create('test', ['a', 'b', 'c', 'd'], (worker, id) => {
setTimeout(() => {
console.log('Test =>', id);
worker.pop();
}, (~~(Math.random() * 1000)));
}).run();
workers.test.complete(() => {
console.log('All "test" has finished');
});
workers.create('tset', ['z', 'y', 'x', 'w'], (worker, id) => {
setTimeout(() => {
console.log('tset =>', id);
worker.pop();
}, (~~(Math.random() * 1000)));
}, true).run();
workers.tset.complete(() => {
console.log('All "tset" has finished');
});
workers.complete(() => {
console.log('All "workers" has finished');
});
Exemples 2:
const request = require('request');
const babyWorkers = require('baby-workers');
class Main
{
constructor()
{
this.workers = new babyWorkers;
this.workers.create('users', ['58bdza054dre58a9dra56', 'ddz548ftbc5045dee87rr'], this.getUser).save([]).run();
this.workers.users.complete((error) => {
if (error !== null) {
return ;
}
console.log('Users:', this.workers.users.get());
});
this.workers.create('articles', ['45dz54dez50dez84fzsd', 'bvc0b4b8fdfdg48dfgfd1', 'd48d4z0g4v8cx4q8sc4q'], this.getArticles).save([]).run();
this.workers.articles.complete((error) => {
if (error !== null) {
return ;
}
console.log('Articles:', this.workers.articles.get());
});
this.workers.complete((error, fatalError) => {
if (error !== null) {
console.log(error);
console.error(fatalError);
return ;
}
console.log('All workers has finished');
});
}
getUser(worker, idUser)
{
request({
url: urlGetUser + idUser,
}, (error, response, body) => {
if (error !== null) {
worker.error('Get user ' + idUser + ' error', error);
return worker.pop();
}
worker.save(body);
worker.create('notifyUser', [idUser], this.putNotifyUser).run();
worker.create('billUser', body.data.bills, this.getBillUser).save([]).run();
worker.billUser.complete(() => {
worker.root().save(worker.root().get().concat([{
firstName: body.data.firstName,
lastName: body.data.lastName,
bills: worker.billUser.get(),
}]));
worker.pop();
});
});
}
putNotifyUser(worker, idUser)
{
request({
url: urlNotifyUser + idUser,
}, (error, response, body) => {
if (error !== null) {
worker.error('Notify user ' + idUser + ' error', error);
}
worker.pop();
});
}
getBillUser(worker, idBill)
{
request({
url: urlGetBillUser + idBill,
}, (error, response, body) => {
if (error !== null) {
const idUser = worker.root().get().idUser;
worker.error('Billd user ' + idUser + ' error', error);
} else {
worker.root().save(worker.root().get().concat([{
name: body.data.name,
amount: body.data.amount,
}]));
}
worker.pop();
});
}
getArticles(worker, idArticle)
{
request({
url: urlGetArticle + idArticle,
}, (error, response, body) => {
if (error !== null) {
worker.error('Article ' + idArticle + ' error', error);
} else {
worker.root().save(worker.root().get().concat([{
title: body.data.title,
content: body.data.content,
}]));
}
worker.pop();
});
}
}
module.exports = new Main;