# mutex-local

> Mutex lock for multi-process synchronization

Latest version **0.1.2** (published 2022-08-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install mutex-local
pnpm add mutex-local
yarn add mutex-local
bun add mutex-local
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-08-27 |
| First published | 2022-08-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lucas Barrena |
| Maintainers | lukks |

## Links

- npm: https://www.npmjs.com/package/mutex-local
- Repository: https://github.com/LuKks/mutex-local
- Issues: https://github.com/LuKks/mutex-local/issues
- npm.io page: https://npm.io/package/mutex-local

## Dependencies (2)

- [mutexify](https://npm.io/package/mutexify.md) ^1.4.0
- [fs-native-extensions](https://npm.io/package/fs-native-extensions.md) ^1.1.0

## Recent versions

- 0.1.2 (latest) — 2022-08-27
- 0.1.1 — 2022-08-27
- 0.1.0 — 2022-08-27

## README

# mutex-local

Mutex lock for multi-process synchronization.

```
npm i mutex-local
```

## Usage
```javascript
const mutexLocal = require('mutex-local')

const mutex = mutexLocal('./filename.lock')

await mutex.waitForLock()
console.log('here do a synced operation between multiple process')
await mutex.unlock()
```

```javascript
const granted = await mutex.tryLock()
if (granted) {
  console.log('here do a synced operation between multiple process')
  await mutex.unlock()
}
```

If you exit the process with a locked mutex then it's automatically released.\
Still do `unlock()` when you can, to avoid file descriptor leaks.

## API

#### `const mutex = mutexLocal(filename)`

Creates a mutex lock based on a filename, so multiple process can coordinate.

#### `await mutex.waitForLock([options])`

Default options:
```js
{
  flags: 'a+',
  mode: 0o666,
  exclusive: true
}
```

#### `const granted = await mutex.tryLock([options])`

Options are the same as for `waitForLock([options])`.

#### `await mutex.unlock()`

Release the lock.

#### `mutex.locked`

Boolean that indicates if mutex is locked or not.

## License
MIT

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