npm.io
1.2.0 • Published 4 months ago

minimum-lock

Licence
GPL-3.0-or-later
Version
1.2.0
Deps
0
Size
4 kB
Vulns
0
Weekly
0

License: GPL v3 Coverage Status

Minimum lock

This package provides a bare minimum tool for "locking" resources.

How to use it?

Installation

You can install the library into your project via npm:

npm install minimum-lock

This package does not have any dependencies.

Usage

Again, this tool is bare minimum, it does not provide any fancy interface, just a class to create an object which can be locked and released.

import { Lock } from 'minimum-lock';

async function doSomethingWithLock(lock) {
  const release = await lock.acquire();
  
  doSomethingMustBeSequential();
  
  release();
}

async function doSomething() {
  ...
  
  const lock = new Lock();
  
  doSomethingWithLock(lock);
  doSomethingWithLock(lock);
}

The acquire method returns a release callback, if you forget to call this release callback, the lock is locked forever.