UNPKG

@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.

59 lines 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryDistributedRegistry = exports.inMemoryDistributedRWLockRegistry = exports.inMemoryDistributedMutexRegistry = exports.inMemoryDistributedSemaphoreRegistry = void 0; const errors_1 = require("../errors"); exports.inMemoryDistributedSemaphoreRegistry = new Map(); exports.inMemoryDistributedMutexRegistry = new Map(); exports.inMemoryDistributedRWLockRegistry = new Map(); class InMemoryDistributedRegistry { static listMutexNames() { return Array.from(exports.inMemoryDistributedMutexRegistry.keys()); } static listSemaphoreNames() { return Array.from(exports.inMemoryDistributedSemaphoreRegistry.keys()); } static listRWLockNames() { return Array.from(exports.inMemoryDistributedRWLockRegistry.keys()); } static clearMutexRegistry() { exports.inMemoryDistributedMutexRegistry.clear(); } static clearSemaphoreRegistry() { exports.inMemoryDistributedSemaphoreRegistry.clear(); } static clearRWLockRegistry() { exports.inMemoryDistributedRWLockRegistry.clear(); } static hasMutex(name) { return exports.inMemoryDistributedMutexRegistry.has(name); } static hasSemaphore(name) { return exports.inMemoryDistributedSemaphoreRegistry.has(name); } static hasRWLock(name) { return exports.inMemoryDistributedRWLockRegistry.has(name); } static getMutex(name) { const mutex = exports.inMemoryDistributedMutexRegistry.get(name); if (!mutex) { throw new errors_1.LockNotFoundError(`Mutex ${name} does not exist`); } return mutex; } static getSemaphore(name) { const semaphore = exports.inMemoryDistributedSemaphoreRegistry.get(name); if (!semaphore) { throw new errors_1.LockNotFoundError(`Semaphore ${name} does not exist`); } return semaphore; } static getRWLock(name) { const rwLock = exports.inMemoryDistributedRWLockRegistry.get(name); if (!rwLock) { throw new errors_1.LockNotFoundError(`ReadWriteLock ${name} does not exist`); } return rwLock; } } exports.InMemoryDistributedRegistry = InMemoryDistributedRegistry; //# sourceMappingURL=in-memory-distributed-registry.js.map