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 🙏

© 2025 – Pkg Stats / Ryan Hefner

data-semaphore

v0.3.10

Published

Lightweight TypeScript/ES2017 class to simulate an asynchronous semaphore, with several utility functions

Downloads

4,587

Readme

Semaphore

Lightweight TypeScript/ES2017 class to simulate an asynchonous semaphore, with several utility functions

Installation

npm install --save data-semaphore

Usage

import { Semaphore } from 'data-semaphore';

const semaphore = new Semaphore( 1 );

const main = async () => {
    const release = await semaphore.acquire();

    try {
        // Critical code we want to limit the concurrency of
        // ...
    } finally {
        // Put the release in the finally block, so that it always runs
        // Despite of possible uncaught exceptions or early return statements
        // Not doing so may result in deadlocks
        release();
    }
};

// Calling the main function multiple times will result in each subsequent call being delayed until the last one has finished,
// Effectively the same as running them sequentially
main();

For this particular use case, where the max concurrent count is one, we can use the shorthand let semaphore = new Mutex(); Using a semaphore with a count bigger than one allows to limit the concurrency, that is, the amount of code protected by that semaphore that can be executed at the same time. Any further calls of execute the same code will wait until a vacancy is available.

We can also create a semaphore for each object. This allows a more fine-grained control when each object should have their own semaphore.

class HeavyUserTasks {
    private semaphore : SemaphorePool<User> = new SemaphorePool<User>( 3 );

    async run ( user : User ) : Promise<T> {
        const release = await this.semaphore.acquire( user );

        try {
            // This way each user can only execute three tasks at the same time
            // ...
        } finally {
            release();
        }
    }
}

Also, a simple way to convert a whole class method is to use the Synchronized decorator.

class HeavyUserTasks {
    // If the count is omitted, the default value will be 1
    // If no getter is provided, a single Semaphore will be used for all calls instead of a SemaphorePool 
    @Synchronized( 3, user => user )
    async run () : Promise<T> {
        // This way each user can only execute three tasks at the same time
        // ...
    }
}

ReadWriteSemaphore

Sometimes it is useful to have two interconnected semaphores, one for reading operations (that might accept infinite concurrent operations) and another for writing operations (that runs sequentially, one at a time). The advantage of this method over two completely separate semaphores is that in this example, the writing operation is blocking, besides any other writing operations, all read operations. And conversely, the writing operation does not occur while read operations are running.

const semaphores = new ReadWriteSemaphore();
semaphores.read // typeof SemaphoreLike
semaphores.write // typeof SemaphoreLike

StateSemaphore

Even more interesting would be being able to design theses state semaphores and specify how they would interact for cases other than reading and writing. For that, there is the StateSemaphore class. For instance, let's create a ReadWriteSemaphore using this class

let states = new StateSemaphore( [
    [ 'read', [], Infinity ]
    [ 'write', [ 'read' ], 1 ]
] );

await states.acquire( 'read' );
await states.acquire( 'write' );

That's it. Additionally, you can also get a semaphore for each state, and use it as any other regular semaphore.

states.getLane( 'read' ) // typeof SemaphoreLike