(fn: () => Promise<T>)
| 27 | * Only one operation can hold the lock at a time. |
| 28 | */ |
| 29 | export async function withStatsCacheLock<T>(fn: () => Promise<T>): Promise<T> { |
| 30 | // Wait for any existing lock to be released |
| 31 | while (statsCacheLockPromise) { |
| 32 | await statsCacheLockPromise |
| 33 | } |
| 34 | |
| 35 | // Create our lock |
| 36 | let releaseLock: (() => void) | undefined |
| 37 | statsCacheLockPromise = new Promise<void>(resolve => { |
| 38 | releaseLock = resolve |
| 39 | }) |
| 40 | |
| 41 | try { |
| 42 | return await fn() |
| 43 | } finally { |
| 44 | // Release the lock |
| 45 | statsCacheLockPromise = null |
| 46 | releaseLock?.() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Persisted stats cache stored on disk. |
no test coverage detected