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

simple-mutex-promise

v1.1.0

Published

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![Build Status](https://www.travis-ci.com/4nduril/simple-mutex.svg?branch=main)](https://www.travis-ci.com/4nduril/simple-mute

Downloads

184

Readme

simple-mutex-promise

Commitizen friendly Build Status

This library provides a very simple mutex/lock implementation in JavaScript.

Usage

The problem

You have an operation / a function that does the following:

  1. A value from outside of the operation is read.
  2. An asynchronous action takes place which leads to giving the execution context to another part of the code.
  3. That other part of the code (can also be another invocation of the same function) does also use the value from outside. Also potentially modifying it.
  4. The first (invocation of the) operation continues and writes a changed value to the same place (variable) that was read from initially.

This kind of code can lead to race conditions where a stale value is read, modified and written back while other parts of the code have already changed it by themselves. One very simple example is shown as unit test case: .

The solution

Before the value in question is read and potentially modified a lock or mutex (mutual exclusion) has to be aquired which blocks (or queues) other parts of the code from accessing the same value. Once the operation is done the lock is released and if other code already tried to access the variable they are informed that execution can continue.

Usage with this library

Install it with npm

npm install simple-mutex-promise

or with yarn

yarn add simple-mutex-promise

In your code import it and create the mutex instance outside of the critical function:

import { getMutex } from 'simple-mutex-promise'

const mutex = getMutex()

It is important that all parts of the code that access the value in question use the same instance returned from getMutex().

Then acquire the lock inside the critical function before any and release it after every usage of the variable is done:


let i = 1

async function criticalAsyncFunction() {
  const [lock, release] = mutex.getLock()
  await lock                               // 1

  const temp = i

  await someAsyncAction()                  // 2
  i = temp + 1

  release()                                // 3
}

Before the outside value is used we await the lock (1). So function execution is paused and only continued when the lock resolves. Then we can read the value and use it (2). In this case we don't use the value in the async action but typically that would be the case. After writing to the outside variable is done we release the lock and now other execution contexts can make use of the value (3). We made sure that other code is only run when all our updates are done.

Types

simple-mutex-promise is written in TypeScript and type declarations are also part of the build. You don't need to install them separately.