A self-hosted orchestration system crafted for power users. composia.xyz
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667type PollingOptions = { intervalMs: number; errorIntervalMs?: number; initialDelayMs?: number; runImmediately?: boolean;};
export function startPolling( tick: () => void | boolean | Promise<void | boolean>, options: PollingOptions,) { let cancelled = false; let timer: ReturnType<typeof setTimeout> | null = null;
const clear = () => { if (timer) { clearTimeout(timer); timer = null; } };
const stop = () => { cancelled = true; clear(); };
const schedule = (delay: number) => { if (cancelled) { return; }
clear(); timer = setTimeout(run, delay); };
const run = async () => { if (cancelled) { return; }
if (typeof document !== "undefined" && document.hidden) { schedule(options.intervalMs); return; }
try { const shouldContinue = await tick(); if (shouldContinue === false) { stop(); return; }
schedule(options.intervalMs); } catch { schedule(options.errorIntervalMs ?? options.intervalMs); } };
if (options.runImmediately) { void run(); } else { schedule(options.initialDelayMs ?? options.intervalMs); }
return stop;}