Skip to content

Mutex

A mutual-exclusion lock for async functions: only one holder at a time, waiters served first in first out.

Import:

import { Mutex } from '@mnaoumov/typescript-lib/mutex';

Example:

const mutex = new Mutex();
await mutex.runExclusive(async () => {
await app.vault.process(file, (content) => content + '\n');
});

Signature:

export class Mutex

Methods

MethodReturnsDescription
acquire(abortSignal?)Promise<void>Takes the lock, waiting until it is free.
release()voidFrees the lock, handing it to the longest-waiting caller if there is one.
runExclusive(operation, abortSignal?)Promise<T>Runs a function while holding the lock, freeing it when the function settles.
tryAcquire()booleanTakes the lock only if it is free right now.