multex

0.1.1 • Public • Published

multex.js

Multi-access mutexes (a.k.a locks, semaphores) with access control strategies (last-only, first-only).

Multexes

  • LastOnly
  • FirstOnly

Examples

Asynchronous callback cancellation

Use a last-only multex to effectively cancel or abort all but the latest callback in any series of asynchronous calls. This is like debouncing, but better, as it should guarantee only one execution immediately after the last async call returns.

var multex = new Multex.LastOnly();
 
[0,1,2].forEach(function () {
  // get key synchrounously, and capture variable in dedicated closure scope
  var key = multex.key();
 
  // start async requests
  setTimeout(function () {
    // in callbacks, try to use key
    if (! key.use()) {
      // abort, key is no longer valid (not the last key in its multex)
      return;
    }
 
    // else proceed, guaranteed this was the last execution
  }, 100);
});

Readme

Keywords

Package Sidebar

Install

npm i multex

Weekly Downloads

18

Version

0.1.1

License

ISC

Unpacked Size

3.46 kB

Total Files

4

Last publish

Collaborators

  • adjohnson916