latches

1.0.9 • Public • Published

Latches

A library of latches useful for managing concurrency

Latch

var latch = new Latches.Latch(stack, [opened, [context]]);
 
latch.wait(function() { ... }, [context]);
 
latch.open(context);
 
latch.close();
  • stack is an Array of functions. If a member is not a function it is filtered out. They are immediately added as waiting on the latch. The default is an empty array.
  • opened is boolean. Any given value is cast to a boolean. It is the starting position of the latch. The default is false, closed.
  • context is an object to be used as the context for any waiting functions being called as a context for the action.

All parameters are optional. context only matters if opened is true.

The plain latch is typically used to build a processing queue for when a service is unavailable, to be submitted when the service becomes available again. In such cases it is important to wrap the functions given to the latch so that if the service isn't available, or stops being available, when they are released the latch is closed again and they are added back to it.

CountDownLatch

var cdl = new Latches.CountDownLatch(size, [stack, [context]]);
 
cdl.wait(function() { ... }, [context]);
 
cdl.hit([context]);
 
cdl.add(amount, [context]);
 
cdl.wait

Readme

Keywords

Package Sidebar

Install

npm i latches

Weekly Downloads

346

Version

1.0.9

License

MIT

Last publish

Collaborators

  • gaussflayer