0.1.1 • Published 5 years ago

multex v0.1.1

Weekly downloads
2
License
ISC
Repository
-
Last release
5 years ago

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