UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

43 lines 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mutex = void 0; const semaphore_1 = require("./semaphore"); class Mutex { constructor(key) { this.semaphore = semaphore_1.Semaphore.get(key, 1); } async lock() { await this.semaphore.acquire(); } release() { this.semaphore.release(); } static get(key) { if (!Mutex.mutexes.has(key)) { Mutex.mutexes.set(key, new Mutex(key)); } return Mutex.mutexes.get(key); } static async withLock(key, criticalSection) { const mutex = Mutex.get(key); await mutex.lock(); try { return await criticalSection(); } finally { mutex.release(); } } async withLock(criticalSection) { await this.lock(); try { return await criticalSection(); } finally { this.release(); } } } exports.Mutex = Mutex; Mutex.mutexes = new Map(); //# sourceMappingURL=mutex.js.map