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

@eight04/read-write-lock

v0.1.0

Published

A browser-friendly read/write lock.

Downloads

126

Readme

read-write-lock

Build Status codecov

A browser-friendly read/write lock.

Features

  • Read/write lock.
  • Include a lock pool which can acquire multiple keys at once.
  • No process.nextTick. Built with Promise.
  • The pending queue is implemented with a linked list.
  • Support promise while it is allowed to use a done callback.
  • Throttle read tasks.

Installation

npm:

npm install @eight04/read-write-lock
const {createLock, createLockPool} = require("@eight04/read-write-lock");
...

CDN:

<script src="https://unpkg.com/@eight04/read-write-lock/dist/read-write-lock.min.js"></script>
/* global readWriteLock */
const {createLock, createLockPool} = readWriteLock;
...

Usage

createLock:

const lock = createLock();
lock.read(async () => {
  console.log(1);
  await delay(100);
});
lock.read(async () => {
  console.log(2);
  await delay(100);
});
lock.write(async () => {
  console.log(3);
  await delay(100);
});
lock.read(async () => {
  console.log(4);
  await delay(100);
});
1
2
<100ms delay>
3
<100ms delay>
4

createLockPool:

const lockPool = createLockPool();
lockPool.read(["foo", "bar"], async () => {
  console.log(1);
  await delay(100);
});
lockPool.write(["bar"], async () => {
  console.log(2);
  await delay(100);
});
lockPool.write(["baz"], async () => {
  console.log(3);
  await delay(100);
});
lockPool.read(["foo"], async () => {
  console.log(4);
  await delay(100);
});
1
3
4
<100ms delay>
2

API

This module exports two functions:

  • createLock - create a lock object that can be used to queue up async tasks.
  • createLockPool - create a lock pool that can queue up async tasks for different scopes.

createLock

const lock = createLock({
  maxActiveReader = Infinity
} = {});

Create a read/write lock.

maxActiveReader controls how many reader will run in parallel.

lock.read

const callbackResult = await lock.read(callback: Function | AsyncFunction);

Register a reader callback, wait until the reader get called, and return the callback result.

If callback accepts no argument, the lock will be released when the callback returns. Otherwise, callback accepts a release function that will release the lock when called.

If callback is a sync function and it throws when called, the lock will be released immediately.

lock.write

const callbackResult = await lock.write(callback: Function | AsyncFunction);

Register a writer callback, wait until the writer get called, and return the callback result.

createLockPool

const pool = createLockPool(options?);

Create a lock pool. You can operate on multiple locks at once by specifying multiple scopes.

options object will be sent to createLock.

pool.read

const callbackResult = await pool.read(scopes: Iterable, callback: Function | AsyncFunction);

Grant access to multiple scopes, wait until the reader get called, and return the callback result.

scopes may contain anything that can be used as the key of JavaScript Map.

pool.write

const callbackResult = await pool.write(scopes: Iterable, callback: Function | AsyncFunction);

Grant access to multiple scopes, wait until the writer get called, and return the callback result.

Similar projects

There are a lot of other implementations on npm:

  • read-write-lock - simple read/write lock built with mutexify which uses process.nextTick.
  • node-memory-lock - support priority/upgrade/downgrade/timeout.
  • async-rwlock - the unlock timing is hidden. Support timeout.
  • rwlock - the unlock function is sync.
  • mortice - put the lock in worker/cluster. Support timeout/throttle.
  • readwrite-lock - promise based. Support limited queue size/timeout/priority.

Changelog

  • 0.1.0 (Apr 5, 2019)

    • First release.