1const DEFAULT_TIMEOUT_MS = 2_000;
2const DEFAULT_POLL_INTERVAL_MS = 40;
3
4export async function waitForThreadShellReady(input: {
5 readonly read: () => boolean;
6 readonly timeoutMs?: number;
7 readonly pollIntervalMs?: number;
8 readonly now?: () => number;
9 readonly delay?: (durationMs: number) => Promise<void>;
10}): Promise<boolean> {
11 const now = input.now ?? Date.now;
12 const delay =
13 input.delay ??
14 ((durationMs: number) =>
15 new Promise<void>((resolve) => {
16 setTimeout(resolve, durationMs);
17 }));
18 const deadline = now() + (input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
19 const pollIntervalMs = input.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
20
21 while (!input.read() && now() < deadline) {
22 await delay(Math.min(pollIntervalMs, deadline - now()));
23 }
24
25 return input.read();
26}