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

Execute and manage your code asynchronously with workers, like promise, execute each element of an array or a simple element in callback.

  • 2.0.2
  • Source
  • npm
  • Socket score

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

Baby workers Javascript

Execute and manage your code asynchronously with workers, like 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

Last release

VersionChange logCompatibility
2.0.0Add map, set, push (data), next, exec, reply and getError functions + fix some bugs>= 2.0.0
2.0.0 betaAdd promise after run/timeout/interval/stack function + add then/catch callback + catch error in run/timeout/interval/stack callback + add feature to create worker without name>= 2.0.0
1.0.71Add getNodes function<= 1.0.71

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) => {
    console.log('|=>', elem);
    worker.pop();
}).map(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise
    console.log('Then');
}).catch(() => { // catch is a promise
    console.log('Catch');
});

workers.create('MyWorker2', (worker, elem) => {
    console.log('|=>', elem);
    worker.pop();
}).set(['a', 'b', 'c', 'd']).run().then(() => { // then is a promise
    console.log('Then');
}).catch(() => { // catch is a promise
    console.log('Catch');
});

workers.then(() => { // then is not a promise
    console.log('Then');
}).catch(() => { // catch is not a promise
    console.log('Catch');
}).complete(() => {
    console.log('Complete');
});

Demos

  • Basic
  • Promise
  • Stack
  • Simulate adding/removing worker
  • Set delay
  • Set interval
  • Push
  • Adding data
  • Cancel worker
  • Limit workers with a queue
  • Set error
  • All

Main functions

FunctionAvailableDescriptionAdditionnal
create(name: string or callback: function, callback: function) : currentWorkerALLCreate 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) : currentWorkerALLSet 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). If extra = true is true so maxWorkers is taken ONLY if parent workers limit is full
addWorker() : currentWorkerALLAdd virtual worker (be careful !)
removeWorker() : currentWorkerALLRemove virtual worker (be careful !)

Callback functions

FunctionAvailableDescriptionAdditionnal
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 : objectALLIS NOT A FUNCTION BUT AN OBJECT !

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
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)

Keywords

FAQs

Package last updated on 19 Feb 2018

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