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

lockbase

v5.0.2

Published

A queued locking library useful for databases

Downloads

36

Readme

lockbase

Node.js Test Runner GitHub code size in bytes GitHub package.json version GitHub

A queued locking library useful for databases.

Installation

npm install --save lockbase

Usage

The lockbase module has the following methods:

  • add a new lock
  • remove an existing lock
  • cancel all locks
  • find all locks
  • wait for key to have no more locks
  • importState
  • exportState
  • on for adding a listener
  • off for removing a listener
import lockbase from 'lockbase';
const locks = lockbase();

// Add a lock, and wait until it becomes active
const lockId = await locks.add(
  'users', // path to lock
  {
    id: 'optional custom id' // defaults to increments starting at 1,
    somethingElse: 'abc' // add custom metadata
  }
);

// Remove a lock when you're finished with it
locks.remove(lockId);

const usersLocks = locks.find('users');
/*
usersLocks === [{
  id: 1,
  path: 'users'
}]
*/

// Wait until a key has no locks associated with it
// This will wait until `users` is unlocked.
await locks.wait('users.email')

// Cancel all locks, rejecting any promises that are waiting.
locks.cancel(new Error('server is closing down'))

Queue and Events

The queue holds all active locks, future locks and waits.

The lockbase module is actually an EventEmitter that emits two events:

  • queue:insert when an item is added to the queue
  • queue:remove when an item is removed
  • resolved:{id} when an item is resolved
locks.on('queue.insert', item => {
  console.log('item has been inserted', item);
});

locks.on('queue.remove', item => {
  console.log('item has been removed', item);
});

locks.on('resolved.abcd', item => {
  console.log('item abcd has been resolved', item);
});


locks.on('change', ({ item, event } => {
  console.log(`item has been ${event}`, { event, item });
});

Export lock state

If you are running multiple servers, where a primary server is used, you may need to hand over lock state. For example, if using raft, following a leader election.

You can export the lock state as a JSON object and import it into another server.

const exportedState = locks1.exportState();
/*
exportedState === {
    queue: [{
      id: 1,
      path: 'users'  
    }],
    incremental: 1
  }
*/

locks2.importState(exportedState);

Paths

Paths follow dot notation and will match partially.

  • users will match users, users.email, users.anythingElse
  • users.email will match users.email and users.email.subpath

Example

const lockbase = require('lockbase');
const locks = lockbase();

locks.add('users').then(lock => {
  const isLocked = locks.find('users.email')
  console.log(isLocked) // === true
  setTimeout(() => locks.remove(lock), 500);
});

locks.add('users').then(lock => {
  // This will not be called for 500ms
  locks.remove(lock);
});