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

alock

v0.0.2

Published

Arbitrary locking mechanism

Downloads

5

Readme

alock

Arbitrary locking mechanism for asynchronous operations

About

npm install alock

This module is an event-based approach to locking asynchronous operations. Locks are established and freed on a key-by-key basis and are passed two arguments: a key to uniquely identify the operation you want to lock, and a callback.

This callback should accept a function – done() – which, when invoked, frees the lock.

Let's give it a whirl.

Usage

var lock = require('alock');

lock("console-log", function (done) {
  setTimeout(function () {
    console.log('First!');
    done();
  }, 1000);
});

lock("console-log", function (done) {
  setImmediate(function () {
    console.log('Second!');
    done();
  });
});

Without locking, the above example would normally print "Second!" followed by "First!" a second later. Instead, we see a 1-second delay, followed by both "First!" and "Second!" displaying in quick succession.

alock can be used to simulate atomic operations on virtually anything. Consider the following example: We want to fetch some serialized object from a key-value store, change one of its properties, and place it back. If two of these operations are called in quick succession, one of the operations may receive an outdated copy of this object because of a race condition. Instead, let's lock the operation to prevent such a data hazard.

lock("double-score", function (done) {
  db.get('player1', function (err, value) {
    value = JSON.parse(value);
    value.score *= 2;
    db.put('player1', JSON.stringify(value), function (err) {
      done();
    });
  });
});

How it Works

alock uses node's built-in EventEmitter class at its core. The internal locks object holds EventEmitter instances, each responsible for emitting a "free" event when the operation has completed.

When lock() is called, the library checks to see if the key already has a lock on it – that is – that the internal locks object holds the key. If such a lock already exists, we create an event listener for the "free" event to then acquire the lock ourselves.

To acquire a lock means to assign a new EventEmitter instance to the key in the internal locks object, then invoke the callback originally passed to us.

The source is around 30 lines, so be sure to check it out for yourself.

License

MIT