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