@apiratorjs/locking
Version:
A lightweight library providing both local and distributed locking primitives (mutexes and semaphores) for managing concurrency in Node.js.
44 lines • 1.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryDistributedMutex = void 0;
const in_memory_distributed_semaphore_1 = require("./in-memory-distributed-semaphore");
const in_memory_distributed_registry_1 = require("./in-memory-distributed-registry");
const node_crypto_1 = __importDefault(require("node:crypto"));
class InMemoryDistributedMutex {
constructor(props) {
this.implementation = "in-memory";
this._inMemoryDistributedSemaphore = new in_memory_distributed_semaphore_1.InMemoryDistributedSemaphore({
maxCount: 1,
name: props.name
}, in_memory_distributed_registry_1.inMemoryDistributedMutexRegistry, "mutex");
}
get name() {
return this._inMemoryDistributedSemaphore.name;
}
;
get isDestroyed() {
return this._inMemoryDistributedSemaphore.isDestroyed;
}
async runExclusive(...args) {
// @ts-ignore
return this._inMemoryDistributedSemaphore.runExclusive(...args);
}
async destroy() {
return this._inMemoryDistributedSemaphore.destroy("Mutex destroyed");
}
acquire(params, acquireToken) {
const token = `${this.name}:${node_crypto_1.default.randomUUID()}`;
return this._inMemoryDistributedSemaphore.acquire(params, acquireToken ?? token);
}
cancel(errMessage) {
return this._inMemoryDistributedSemaphore.cancelAll(errMessage);
}
isLocked() {
return this._inMemoryDistributedSemaphore.isLocked();
}
}
exports.InMemoryDistributedMutex = InMemoryDistributedMutex;
//# sourceMappingURL=in-memory-distributed-mutex.js.map