# mortice

> Isomorphic read/write lock that works in single processes, node clusters and web workers

Latest version **3.3.1** (published 2025-06-05) · Apache-2.0 OR MIT license · 0 weekly downloads

## Install

```sh
npm install mortice
pnpm add mortice
yarn add mortice
bun add mortice
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.3.1 |
| Published | 2025-06-05 |
| First published | 2018-05-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 OR MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 380.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 9 |
| Author | Alex Potsides |
| Maintainers | achingbrain |
| Keywords | async, await, cluster, lock, mutex, read-write |

## Links

- npm: https://www.npmjs.com/package/mortice
- Repository: https://github.com/achingbrain/mortice
- Homepage: https://github.com/achingbrain/mortice#readme
- Issues: https://github.com/achingbrain/mortice/issues
- npm.io page: https://npm.io/package/mortice

## Dependencies (3)

- [it-queue](https://npm.io/package/it-queue.md) ^1.1.0
- [main-event](https://npm.io/package/main-event.md) ^1.0.0
- [abort-error](https://npm.io/package/abort-error.md) ^1.0.0

## 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

- 3.3.1 (latest) — 2025-06-05
- 3.3.0 — 2025-06-05
- 3.2.1 — 2025-06-02
- 3.2.0 — 2025-06-02
- 3.1.0 — 2025-05-28
- 3.0.6 — 2024-10-24
- 3.0.5 — 2024-10-24
- 3.0.4 — 2023-12-18
- 3.0.3 — 2023-12-08
- 3.0.2 — 2023-12-08
- 3.0.1 — 2022-08-17
- 3.0.0 — 2022-02-09
- 2.0.1 — 2021-02-06
- 2.0.0 — 2019-09-06
- 1.2.3 — 2019-08-22
- … 8 more at https://npm.io/package/mortice/versions

## README

# mortice

[![codecov](https://img.shields.io/codecov/c/github/achingbrain/mortice.svg?style=flat-square)](https://codecov.io/gh/achingbrain/mortice)
[![CI](https://img.shields.io/github/actions/workflow/status/achingbrain/mortice/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/achingbrain/mortice/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> Isomorphic read/write lock that works in single processes, node clusters and web workers

# About

<!--

!IMPORTANT!

Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.

To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts

To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.

-->

- Reads occur concurrently
- Writes occur one at a time
- No reads occur while a write operation is in progress
- Locks can be created with different names
- Reads/writes can time out

## Example

```ts
import mortice from 'mortice'
import delay from 'delay'

// the lock name & options objects are both optional
const mutex = mortice()

Promise.all([
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 1')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 2')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.writeLock()

    try {
      await delay(1000)

      console.info('write 1')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 3')
    } finally {
      release()
    }
  })()
])
```

```
read 1
read 2
<small pause>
write 1
read 3
```

## Clean up

Mutexes are stored globally reference by name, this is so you can obtain the
same lock from different contexts, including workers.

When a mutex is no longer required, the `.finalize` function should be called
to remove any internal references to it.

```ts
import mortice from 'mortice'

const mutex = mortice()

// ...some time later

mutex.finalize()
```

## Auto clean up

If your app generates a lot of short-lived mutexes and you want to clean them
up after the last lock has been released, pass the `autoFinalize` option to
mortice in the owning context:

```ts
import mortice from 'mortice'

const mutex = mortice({
  autoFinalize: true
})

const release = await mutex.readLock()
// ...some time later

release()

// mutex will be freed soon after
```

## React native support

This module should run on react native but it only supports single-process
concurrency as it's not clear to the author (disclaimer - not a react native
dev) what the officially supported process concurrency model is.

Please open an issue if this is a feature you would like to see added.

# Install

```console
$ npm i mortice
```

## Browser `<script>` tag

Loading this module through a script tag will make its exports available as `Mortice` in the global namespace.

```html
<script src="https://unpkg.com/mortice/dist/index.min.js"></script>
```

# API Docs

- <https://achingbrain.github.io/mortice>

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](https://github.com/achingbrain/mortice/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/achingbrain/mortice/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

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