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

@shopify/semaphore

v3.1.0

Published

Counting semaphore

Downloads

202,930

Readme

@shopify/semaphore

Build Status Build Status License: MIT npm version npm bundle size (minified + gzip)

The Semaphore class implements a counting semaphore. It can be useful to control concurrent access to a pool of resources such as:

  1. Maintaining a pool of web workers to run background scripts
  2. Limiting the number of concurrent requests that can be made to an API endpoint

In more concrete terms, if we take a semaphore with count 3 as an example, the first 3 calls to acquire a permit will resolve immediately and the 4th call will only be resolved when one of the earlier permits is released.

A real-life anology is parking spots at a parking lot. If the parking lot has a capacity for 10 cars, the first 10 cars to arrive will immediately park, but an 11th car will have to wait for one of the cars to leave so that a parking spot is available.

Installation

yarn add @shopify/semaphore

Usage

Instantiation

Create a semaphore instance by calling the Semaphore constructor with a count argument:

const semaphore = new Semaphore(3);

If you need a lock/mutex, a semaphore with a count of 1 will effectively act as one:

const mutex = new Semaphore(1);

Acquiring permits

Call .acquire() on a Semaphore instance to acquire a permit. The result is a promise that gets resolved with a Permit instance when a permit is available:

const permit = semaphore.acquire();

Releasing permits

Call the .release() method on a Permit instance to release it:

permit.release();

The .release)() method returns a promise that gets resolved when the permit is released and an earlier permit request that had been pending is resolved. Waiting on the resolution of the .release() is optional and could be useful in situations where you're having timing issues (e.g. in unit tests that utilize a Semaphore instance):

await permit.release();

Example

const MAX_SIMULTANEOUS_FETCHES = 2;

const fetchSemaphore = new Semaphore(MAX_SIMULTANEOUS_FETCHES);

async function callApi(path) {
  const permit = await fetchSemaphore.acquire();

  return fetch(path).finally(() => permit.release());
}

callApi(apples).then(renderApples);
callApi(oranges).then(renderOranges);
// The next acquire call won't resolve until one of the earlier permits is released
callApi(bananas).then(renderBananas);