@apiratorjs/locking
Version:
A lightweight library providing both local and distributed locking primitives (mutexes, semaphores, and read-write locks) for managing concurrency in Node.js.
33 lines • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mutex = void 0;
const semaphore_1 = require("./semaphore");
class Mutex {
constructor() {
this._semaphore = new semaphore_1.Semaphore(1);
}
async runExclusive(...args) {
// @ts-ignore
return this._semaphore.runExclusive(...args);
}
async acquire(params, acquireToken) {
// Cast SemaphoreToken to MutexToken since we're implementing a mutex interface
const releaser = await this._semaphore.acquire(params, acquireToken);
const wrappedReleaser = {
release: async () => releaser.release(),
getToken: () => releaser.getToken()
};
return wrappedReleaser;
}
async cancel(errMessage) {
return this._semaphore.cancelAll(errMessage ?? "Mutex cancelled");
}
async isLocked() {
return this._semaphore.isLocked();
}
async waitForUnlock() {
return await this._semaphore.waitForAnyUnlock();
}
}
exports.Mutex = Mutex;
//# sourceMappingURL=mutex.js.map