(
client: HyperframesCloudClient,
renderId: string,
options: PollOptions = {},
)
| 54 | * on a retry inside the poll window. |
| 55 | */ |
| 56 | export async function pollUntilTerminal( |
| 57 | client: HyperframesCloudClient, |
| 58 | renderId: string, |
| 59 | options: PollOptions = {}, |
| 60 | ): Promise<HyperframesRenderDetail> { |
| 61 | const intervalMs = options.intervalMs ?? DEFAULT_POLL_INTERVAL_MS; |
| 62 | const maxWaitMs = options.maxWaitMs ?? DEFAULT_MAX_WAIT_MS; |
| 63 | const now = options.now ?? (() => Date.now()); |
| 64 | // Default sleep honors the abort signal so Ctrl+C feels immediate |
| 65 | // instead of waiting out the full interval. Tests inject a no-op |
| 66 | // sleep that ignores the signal — that's fine, they don't abort. |
| 67 | const sleep = options.sleep ?? defaultAbortableSleep(options.signal); |
| 68 | |
| 69 | const started = now(); |
| 70 | |
| 71 | while (true) { |
| 72 | if (options.signal?.aborted) { |
| 73 | throw signalAbortError(options.signal); |
| 74 | } |
| 75 | const detail = await client.getRender({ render_id: renderId, signal: options.signal }); |
| 76 | const elapsed = now() - started; |
| 77 | options.onTick?.(detail, elapsed); |
| 78 | |
| 79 | if (isTerminal(detail.status)) { |
| 80 | return detail; |
| 81 | } |
| 82 | if (elapsed >= maxWaitMs) { |
| 83 | throw new PollTimeoutError(detail, elapsed); |
| 84 | } |
| 85 | await sleep(intervalMs); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | function signalAbortError(signal: AbortSignal): Error { |
| 90 | const reason = signal.reason; |
no test coverage detected