mortice
Isomorphic read/write lock that works in single processes, node clusters and web workers
Last updated 10 months ago by achingbrain .
Apache-2.0 OR MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mortice 
SYNC missed versions from official npm registry.

mortice

codecov CI

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

About

  • 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

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.

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:

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

$ npm i mortice

Browser <script> tag

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

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

API Docs

License

Licensed under either of

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.

Current Tags

  • 3.3.1                                ...           latest (10 months ago)

23 Versions

  • 3.3.1                                ...           10 months ago
  • 3.3.0                                ...           10 months ago
  • 3.2.1                                ...           10 months ago
  • 3.2.0                                ...           10 months ago
  • 3.1.0                                ...           10 months ago
  • 3.0.6                                ...           a year ago
  • 3.0.5                                ...           a year ago
  • 3.0.4                                ...           2 years ago
  • 3.0.3                                ...           2 years ago
  • 3.0.2                                ...           2 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           7 years ago
  • 1.2.3                                ...           7 years ago
  • 1.2.2                                ...           7 years ago
  • 1.2.1                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (7)

Copyright 2013 - present © cnpmjs.org | Home |