Skip to content

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

License

Notifications You must be signed in to change notification settings

aromatt/lockwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 impose 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 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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published