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

@ncpa0cpl/mutex.js

v1.0.1

Published

Mutual Exclusion mechanism for Asynchronous JS Code.

Downloads

10

Readme

Mutex.js

Mutual Exclusion mechanism for Asynchronous JS Code.

Mutex

Usage of the Mutex is plain and simple. Mutex class provided by this package has only two methods available, acquire() and release().

  • acquire() should be called and awaited before accessing the shared resource.

  • release() should be called after acquiring and finishing the action on the shared resource.

Example

The most simple usage of the Mutex class

import { Mutex } from "mutex.js";

class Foo {
  private readonly mutex = new Mutex();
  private sharedResource = {};

  async actionOnResource() {
    await this.mutex.acquire();

    try {
      // do something with `this.sharedResource`
    } finally {
      this.mutex.release();
    }
  }
}

A function that takes a callback and executes it with an acquired Mutex lock

import { Mutex } from "mutex.js";

const mutex = new Mutex();

async function transaction<R>(callback: () => Promise<R>) {
  await mutex.acquire();

  try {
    return await callback();
  } finally {
    mutex.release();
  }
}

// ...

const foo: string = await transaction(async () => {
  // some async code
  return "success";
});

RWMutex

RWMutex (or read/write mutex) is specialized mutex type the allows for multiple read operations to happen simultaneously, but write operations are always atomic in relation to every other operation.

It works similarly to the regular Mutex, but each "acquire" and "release" must specify if the operation is a read or a write.

Methods it exposes are:

  • acquireWrite() should be called and awaited before writing to the shared resource.

  • releaseWrite() should be called after acquiring and finishing the write operation on the shared resource.

  • acquireRead() should be called and awaited before reading the shared resource.

  • releaseRead() should be called after acquiring and finishing the reading operation on the shared resource.

Example

class DBConnection {
  private mutex = new RWMutex();

  // Writes to the DB
  async insert() {
    await this.mutex.acquireWrite();

    try {
      // <put your async code here>
    } finally {
      this.mutex.releaseWrite();
    }
  }

  // Read from the DB
  async select() {
    await this.mutex.acquireRead();

    try {
      // <put your async code here>
    } finally {
      this.mutex.releaseRead();
    }
  }
}

What's a Mutual Exclusion (ie. Mutex)?

Mutual exclusion is a property of process synchronization which states that “no two processes can exist in the critical section at any given point of time”. The term was first coined by Djikstra. Any process synchronization technique being used must satisfy the property of mutual exclusion, without which it would not be possible to get rid of a race condition.

Source: https://www.geeksforgeeks.org/mutual-exclusion-in-synchronization/

In the context of this library this means that any code put between the acquire() and release() will only be executed if there is no other instance of code running that's also put between the acquire() and release() of the same Mutex object. And any attempts to run said code in that situation will be delayed until it is possible to run it, as per the mutual exclusion rule.