Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445import type { ImportState } from "$lib/api/repo";
interface ImportPollOptions { fetchState: () => Promise<ImportState | null>; onState: (state: ImportState | null) => void; onSettled?: () => void; minDelayMs?: number; maxDelayMs?: number;}
const MIN_DELAY_MS = 4_000;const MAX_DELAY_MS = 60_000;
export const startImportPoll = ({ fetchState, onState, onSettled, minDelayMs = MIN_DELAY_MS, maxDelayMs = MAX_DELAY_MS}: ImportPollOptions): (() => void) => { let delay = minDelayMs; let timer: ReturnType<typeof setTimeout> | undefined; let cancelled = false;
const recheck = async () => { const next = await fetchState(); if (cancelled) return; // a null answer means the import finished and bobbin dropped the record; reporting it is what // dismisses the banner, so it must not be skipped onState(next); if (!next || next.content === "failed") { onSettled?.(); return; } delay = Math.min(delay * 2, maxDelayMs); timer = setTimeout(recheck, delay); };
timer = setTimeout(recheck, delay); return () => { cancelled = true; if (timer) clearTimeout(timer); };};