0.1.2 • Published 10 months ago

lockwrap v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

lockwrap

Funnel a function's invocations into a queue or guard them with a nonblocking mutex.

queued

Forces a function's calls into a queue, causing them to execute sequentially.

Example:

let serialFetch = queued(function(release, a) {

  api.fetch(a, function(result) {

    process(result);  // Do something with result
    release();        // Release exclusive lock

  }, release);        // We provide `release` as an error callback to `api.fetch`
                      // so that the lock will be released even if the find
                      // fails
});

Adding a timeout

You can enforce a timeout on exclusivity by adding { timeout: <milliseconds> } as an argument to queued().

Example:

let serialFn = queued(function(release) {
  doLengthyWork();
  release();
}, { timeout: 100 });

Here, the release callback will be automatically called after 100 milliseconds, allowing the next call to be executed even if the current one is still in progress.

passThrough

Wraps the provided function in a limited-lifespan, nonblocking mutex, preventing reentry into the function until the provided release callback is called.

While an execution of the function is in progress, all would-be concurrent calls are thrown out.

passThrough accepts a timeout as well via { timeout: <milliseconds> }.

TODO

  • switch to Promises and return function's results / error
  • support custom release/timeout functions
  • optionally raise an exception when lock cannot be obtained
0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago