UNPKG

@whiskeysockets/baileys

Version:

A WebSockets library for interacting with WhatsApp Web

33 lines 957 B
import { Mutex as AsyncMutex } from 'async-mutex'; export const makeMutex = () => { const mutex = new AsyncMutex(); return { mutex(code) { return mutex.runExclusive(code); } }; }; export const makeKeyedMutex = () => { const map = new Map(); return { async mutex(key, task) { let entry = map.get(key); if (!entry) { entry = { mutex: new AsyncMutex(), refCount: 0 }; map.set(key, entry); } entry.refCount++; try { return await entry.mutex.runExclusive(task); } finally { entry.refCount--; // only delete it if this is still the current entry if (entry.refCount === 0 && map.get(key) === entry) { map.delete(key); } } } }; }; //# sourceMappingURL=make-mutex.js.map