# @slimio/lock

> SlimIO Asynchronous Lock Handler

Latest version **1.0.0** (published 2021-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @slimio/lock
pnpm add @slimio/lock
yarn add @slimio/lock
bun add @slimio/lock
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-08-14 |
| First published | 2019-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | SlimIO |
| Maintainers | fraxken, alexandre.malaj, ahkrin, mcroquet |
| Keywords | lock, mutex, asynchronous |

## Links

- npm: https://www.npmjs.com/package/@slimio/lock
- Repository: https://github.com/SlimIO/Lock
- Homepage: https://github.com/SlimIO/Lock#readme
- Issues: https://github.com/SlimIO/Lock/issues
- npm.io page: https://npm.io/package/@slimio/lock

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-08-14
- 0.5.0 — 2020-03-01
- 0.4.0 — 2020-01-26
- 0.3.0 — 2019-09-13
- 0.2.0 — 2019-08-06
- 0.1.0 — 2019-06-13

## 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)
![dep](https://img.shields.io/david/SlimIO/Lock)
![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/) v12 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

<details><summary>acquireOne(): Promise< () => void ></summary>
<br />
Create a new lock counter. Return a function that you need to execute to free the counter/lock.

</details>

<details><summary>freeOne(error?: Error): void</summary>
<br />
free an acquired lock (or do nothing if there is no lock acquired yet).

</details>

<details><summary>rejectAll(errorMessage?: string): void</summary>
<br />
Reject all promises and available locks. This will throw an Error for each called acquireOne. The default error is `Lock acquisition rejected!`.

</details>

<details><summary>reset(): void</summary>
<br />
Reset the Object (will reject if any locks or promises are detected active).

</details>

## Dependencies

This project have no dependencies.

## License
MIT

---
_Source: https://npm.io/package/@slimio/lock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
