| 714 | } |
| 715 | |
| 716 | private async drainLock(lock: AsyncLock, timeoutMs: number): Promise<boolean> { |
| 717 | if (timeoutMs <= 0) { |
| 718 | return false |
| 719 | } |
| 720 | |
| 721 | return await new Promise<boolean>((resolve) => { |
| 722 | let settled = false |
| 723 | let timeout: ReturnType<typeof setTimeout> | null = null |
| 724 | |
| 725 | const finish = (value: boolean) => { |
| 726 | if (settled) return |
| 727 | settled = true |
| 728 | if (timeout) { |
| 729 | clearTimeout(timeout) |
| 730 | } |
| 731 | resolve(value) |
| 732 | } |
| 733 | |
| 734 | timeout = setTimeout(() => finish(false), timeoutMs) |
| 735 | |
| 736 | lock.inLock(async () => { }) |
| 737 | .then(() => finish(true)) |
| 738 | .catch(() => finish(false)) |
| 739 | }) |
| 740 | } |
| 741 | |
| 742 | async flush(options?: { timeoutMs?: number }): Promise<void> { |
| 743 | const deadlineMs = Date.now() + (options?.timeoutMs ?? 5_000) |