npm.io
0.1.1 • Published 4 years ago

semaphore-ng

Licence
MIT
Version
0.1.1
Deps
0
Size
7 kB
Vulns
0
Weekly
0

SemaphoreNG

Super simple semaphore implementation written in TypeScript. The concept is similar to semaphore.js.

Installation

$ npm install semaphore-ng
# or
$ yarn add semaphore-ng

Example

// ESM
import { Semaphore } from "semaphore-ng";

// CommonJS
const { Semaphore } = require("semaphore-ng");

const semaphore = new Semaphore(1);

app.get("/", (req, res) => {
    // acquire
    semaphore.acquire(async (release) => {
        const data = await someExpensiveTask();

        // release once done
        release();

        res.send(data);
    });
});

API

[C] new Semaphore(maxCapacity: number);

Creates a semaphore instance.

[M] Semaphore.acquire(cb: (release: ReleaseFunction) => unknown, n?: number);

Acquire and release when the task is done.

[M] Semaphore.call(cb: () => unknown, n?: number);

Same as acquire but release is automatically handled.

[M] Semaphore.release(n?: number);

The release method.

[M] Semaphore.clear();

Clears the queue.

[P] Semaphore.isAvailable(n?: number);

Check for availability.

[P] Semaphore.size;

The queue size.

Keywords