Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/SlimIO/Lock
SlimIO Node.js Semaphore for async/await
https://github.com/SlimIO/Lock
Last synced: 3 months ago
JSON representation
SlimIO Node.js Semaphore for async/await
- Host: GitHub
- URL: https://github.com/SlimIO/Lock
- Owner: SlimIO
- License: mit
- Archived: true
- Created: 2019-06-13T15:28:15.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-05T16:57:03.000Z (about 2 years ago)
- Last Synced: 2024-07-20T20:03:55.121Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 30.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crafted-nodejs - @slimio/lock - Node.js Semaphore like. (Packages / Others)
README
# Lock
![version](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/SlimIO/Lock/master/package.json&query=$.version&label=Version)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/SlimIO/Lock/commit-activity)
![MIT](https://img.shields.io/github/license/mashape/apistatus.svg)
![size](https://img.shields.io/bundlephobia/min/@slimio/lock)
[![Known Vulnerabilities](https://snyk.io//test/github/SlimIO/Lock/badge.svg?targetFile=package.json)](https://snyk.io//test/github/SlimIO/Lock?targetFile=package.json)
[![Build Status](https://travis-ci.com/SlimIO/Lock.svg?branch=master)](https://travis-ci.com/SlimIO/Lock)SlimIO Asynchronous Handler Mutex "Like" Lock. This package has been created to easily lock parallel execution of JavaScript Asynchronous function.
## Requirements
- [Node.js](https://nodejs.org/en/) v14 or higher## Getting Started
This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).
```bash
$ npm i @slimio/lock
# or
$ yarn add @slimio/lock
```## Usage example
```js
const Lock = require("@slimio/lock");const asyncLocker = new Lock({ maxConcurrent: 3 });
async function npmInstall() {
const free = await asyncLocker.acquireOne();
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("npm install resolved!");
}
finally {
free();
}
}// Run 3 per 3 methods
Promise.all([
npmInstall(),
npmInstall(),
npmInstall(),
npmInstall(),
npmInstall()
]).then(() => console.log("all done!")).catch(console.error);
```## API
### Properties
```ts
declare class Lock {
public readonly max: number;
public readonly running: number;
}
```### Methods
acquireOne(): Promise< () => void >
Create a new lock counter. Return a function that you need to execute to free the counter/lock.freeOne(error?: Error): void
free an acquired lock (or do nothing if there is no lock acquired yet).rejectAll(errorMessage?: string): void
Reject all promises and available locks. This will throw an Error for each called acquireOne. The default error is `Lock acquisition rejected!`.reset(): void
Reset the Object (will reject if any locks or promises are detected active).## License
MIT