@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 45 lines 1.26 KB
Raw
1 export interface ShowcaseRetryOptions {
2 readonly isCancelled: () => boolean;
3 readonly attemptTimeoutMs?: number;
4 readonly retryDelayMs?: number;
5 }
6
7 const DEFAULT_ATTEMPT_TIMEOUT_MS = 10_000;
8 const DEFAULT_RETRY_DELAY_MS = 500;
9
10 function delay(ms: number): Promise<void> {
11 return new Promise((resolve) => setTimeout(resolve, ms));
12 }
13
14 async function runAttemptWithTimeout(
15 operation: () => Promise<boolean>,
16 timeoutMs: number,
17 ): Promise<boolean> {
18 let timeout: ReturnType<typeof setTimeout> | null = null;
19 try {
20 return await Promise.race([
21 operation(),
22 new Promise<boolean>((resolve) => {
23 timeout = setTimeout(() => resolve(false), timeoutMs);
24 }),
25 ]);
26 } catch {
27 return false;
28 } finally {
29 if (timeout !== null) clearTimeout(timeout);
30 }
31 }
32
33 export async function retryShowcaseOperation(
34 operation: () => Promise<boolean>,
35 options: ShowcaseRetryOptions,
36 ): Promise<boolean> {
37 const attemptTimeoutMs = options.attemptTimeoutMs ?? DEFAULT_ATTEMPT_TIMEOUT_MS;
38 const retryDelayMs = options.retryDelayMs ?? DEFAULT_RETRY_DELAY_MS;
39
40 while (!options.isCancelled()) {
41 if (await runAttemptWithTimeout(operation, attemptTimeoutMs)) return true;
42 if (!options.isCancelled()) await delay(retryDelayMs);
43 }
44 return false;
45 }