multex
v0.1.1
Published
Multi-access mutexes with access control strategies.
Downloads
40
Readme
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);
});