Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baby-workers

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baby-workers

Manage your functions asynchronously or as stack with baby-workers.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

Workers Javascript

Manage your functions asynchronously or as stack/timeout/interval with baby-workers.

Install

npm install --save baby-workers

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 !

const babyWorkers = require('baby-workers');

const workers = new babyWorkers;
 
// Basic worker
workers.create('basic', (worker, elem) => {
    setTimeout(() => {
        console.log('basic =>', elem, ' - ', 'my id =>', worker.getId());
        worker.pop();
    }, (~~(Math.random() * 1000)));
}, ['a', 'b', 'c', 'd']).run();
workers.basic.complete(() => {
    console.log('All "basic" has finished');
});

// Stack worker 
workers.create('stack', (worker, elem) => {
    setTimeout(() => {
        console.log('stack =>', elem, ' - ', 'my id =>', worker.getId());
        worker.pop();
    }, (~~(Math.random() * 1000)));
}, ['z', 'y', 'x', 'w']).stack(); // mode stack enabled
workers.stack.complete(() => {
    console.log('All "stack" has finished');
});

// Basic worker without array
workers.create('simple', (worker, elem) => {
    setTimeout(() => {
        console.log('simple =>', elem);
        worker.pop();
    }, (~~(Math.random() * 1000)));
}, "toto").run();
workers.simple.complete(() => {
    console.log('All "simple" has finished');
});
 
// Simulate adding a worker
workers.simple.addWorker();
setTimeout(() => {
    console.log('Okay now "simple" is complete');
    workers.simple.removeWorker();
}, 2000);

// Run worker in a timeout
workers.create('timeout', (worker) => {
    console.log('Timeout called');
    worker.pop();
}).timeout(1000);

// Run worker in a setInterval
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);

// Manipule worker data
workers.create('data', (worker) => {
	// var tab = worker.root().get();
	var tab = worker._get(); // new version to get data from root
	tab.push('coucou');
	// worker.root().save(tab);
	worker._save(tab); // new version to save data from root
	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());
    	console.log('Tab ?', worker._get()); // new version to get data from root
    });
  	worker.pop();
}).save([]).run();

// Set errors
workers.create('error', (worker) => {
	worker.error('Why ?', 'Because you stole my bread dude...')
	worker.pop();
}); // .run()

// All workers has finish
workers.complete((error, fatalError) => {
     console.log('All "workers" has finished');
});

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

NameAvailableDescriptionAdditionnal
create(name: string, callback: function, data: any = undefined) : currentWorkerALLCreate a new worker
run() : currentWorkerPARENTRun current worker
stack() : currentWorkerPARENTRun nodes like stack
timeout(time: number = 1) : currentWorkerPARENTRun nodes like run in setTimeout
interval(time: number = 1000) : currentWorkerPARENTRun nodes like run in setIntervalstop() : currentWorker, NODE, Stop interval
getId() : numberNODEGet id of current node worker
getStatus() : stringALLGet status of current worker
getName() : stringALLGet name of current worker
getType() : stringALLReturn type of current worker
pop() : currentWorkerNODEStop current node
addWorker() : currentWorkerALLAdd virtual worker in current worker (it used for external asynch function)
removeWorker(isParent: boolean) : currentWorkerALLRemove virtual worker in current worker (it used for external asynch function)
complete(callback: function, removeAfterCall: boolean) : currentWorkerALLCall function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish)
error(error: string, fatalError: string) : currentWorkerALLSet error in current worker and all parent in the tree
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
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) : nodeWorkerPARENTGet direct node going down the tree

Exemples 2

const request = require('request');
const babyWorkers = require('baby-workers');

class Main
{
	constructor()
	{
		this.workers = new babyWorkers;
		
		this.workers.create('users', this.getUser, ['58bdza054dre58a9dra56', 'ddz548ftbc5045dee87rr']).save([]).run();
		this.workers.users.complete((error) => {
			if (error !== null) {
				return ;
			}
			console.log('Users:', this.workers.users.get());
		});
		
		this.workers.create('articles', this.getArticles, ['45dz54dez50dez84fzsd', 'bvc0b4b8fdfdg48dfgfd1', 'd48d4z0g4v8cx4q8sc4q']).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); // to get idUser from getBillUser
			worker.create('notifyUser', this.putNotifyUser, idUser).run();
			worker.create('billUser', this.getBillUser, body.data.bills).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; // previously saved from getUser
				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;

Keywords

FAQs

Package last updated on 20 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc