(context: ExecutionContext)
| 49 | * @return {FlushLock} Returns a flusher function if a valid context is provided, otherwise undefined. |
| 50 | */ |
| 51 | export function makeFlushLock(context: ExecutionContext): FlushLock { |
| 52 | const registry = getOrCreateFlushLockRegistry(context); |
| 53 | let resolveAllDone: () => void = () => undefined; |
| 54 | const allDone = new Promise<void>(res => { |
| 55 | resolveAllDone = res; |
| 56 | }); |
| 57 | let pending = 0; |
| 58 | |
| 59 | const lock: FlushLockInternal = { |
| 60 | ready: allDone, |
| 61 | acquire: () => { |
| 62 | pending++; |
| 63 | }, |
| 64 | release: () => { |
| 65 | if (--pending === 0) { |
| 66 | registry.locks.delete(lock); |
| 67 | resolveAllDone(); |
| 68 | } |
| 69 | }, |
| 70 | finalize: () => { |
| 71 | if (pending === 0) { |
| 72 | registry.locks.delete(lock); |
| 73 | resolveAllDone(); |
| 74 | } |
| 75 | return allDone; |
| 76 | }, |
| 77 | }; |
| 78 | |
| 79 | registry.locks.add(lock); |
| 80 | return Object.freeze(lock); |
| 81 | } |
| 82 | |
| 83 | function getOrCreateFlushLockRegistry(context: ExecutionContext): FlushLockRegistry { |
| 84 | // eslint-disable-next-line @typescript-eslint/unbound-method |
no test coverage detected