(fn: () => Promise<T>)
| 4 | export const mutexIdMap = new Map<string, Mutex>(); |
| 5 | |
| 6 | export async function execWithMutex<T>(fn: () => Promise<T>): Promise<T> { |
| 7 | let mutex = mutexMap.get(fn); |
| 8 | if (!mutex) { |
| 9 | mutex = new Mutex(); |
| 10 | mutexMap.set(fn, mutex); |
| 11 | } |
| 12 | const releaseLock = await mutex.acquire(); |
| 13 | try { |
| 14 | return await fn(); |
| 15 | } finally { |
| 16 | releaseLock(); |
| 17 | mutexMap.delete(fn); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | export async function execWithMutexId<T>(id: string, fn: () => Promise<T>) { |
| 22 | let mutex = mutexIdMap.get(id); |