npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lockable

v1.0.0

Published

utility module for promises to control their execution and their resolution sequence

Downloads

19

Readme

Build Status Code Climate Coverage Status Documentation

Dependency Status devDependency Status peerDependency Status

lockable

Lockable is a utility module for promises to control their execution and their resolution sequence. It exposes methods that can be used to control the execution order of promises

Usage

// load the lockable library

var lockable = require('lockable');

var keyChain = lockable.acquire('key1.key2');

Public functions

Lockable exposes the below methods that can be used on promises.

acquire(keyChain)

Acquire function is used to acquire a lock on a keychain

A keychain is represents a lock and is a . separated string. A keychain string 'A.B.C' will represent a hierarchy with parents 'A' and 'A.B''.

Acquiring a lock on any parent keychain will invalidate all the child keychain locks. This is detailed in the below examples:

var lockable = require('lockable');

var key1 = lockable.acquire('A.B');

key1() // returns true here

var key2 = lockable.acquire('A.B');

key1() // returns false
key2() // returns true


var key3 = lockable.acquire('A');

key1() // returns false
key2() // returns false
key3() // returns true


var key4 = lockable.acquire('A.B');

key1() // returns false
key2() // returns false
key3() // returns true
key4() // returns true

For more examples on acquire refer the test cases

sequence(keyChain, startCb, endCb)

Sequence is used to sequencify the resolution of promises. Promises added in a sequence will be resolved in the same sequence in which they are added, irrespective of the execution time. Even though the actual promise will be executed instantaneously, the callback will wait for the previous promises to resolve. This is illustrated in the below examples

A sequence can be used if multiple calls update the same object/DOM and only the latest update is relevant.

This is detailed in the below examples:


var sequence = lockable.sequence();
var promise1 = Promise.delay(50);
var promise2 = Promise.delay(60);
var promise3 = Promise.delay(30);

sequence(promise1).then(function() {
    console.log(1);
});

sequence(promise2).then(function() {
    console.log(2);
});

sequence(promise3).then(function() {
    console.log(3);
});

// Output:
// 1 (after 50 ms from start)
// 2 (after 60 ms form start)
// 3 (after 60 ms form start)

For more examples on sequence refer the test cases

queue(keyChain, startCb, endCb)

A queue delays the execution of functions till the return value of previous execution is resolved. This is different from a sequence where, the actual promises are invoked immediately and only the resolution is delayed.

A queue can be used when multiple executions have to happen only one after the other (eg, updating the same records or autosave in a UI form)

This is detailed in the below examples:


var queue = lockable.queue();
queue(function() {
    return Promise.delay(50);
}).then(function() {
    console.log(1);
});
queue(function() {
    return Promise.delay(20);
}).then(function() {
    console.log(1);
});
queue(function() {
    return Promise.delay(30);
}).then(function() {
    console.log(1);
});

// Output:
// 1 (after 50 ms from start)
// 2 (after 70 ms form start)
// 3 (after 100 ms form start)

For more examples on queue refer the test cases