import type { ImportState } from "$lib/api/repo"; interface ImportPollOptions { fetchState: () => Promise; 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 | 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); }; };