2026-03-11 01:40:52 +01:00
|
|
|
export const API = "http://127.0.0.1:8000";
|
|
|
|
|
export const POLL = 15_000;
|
2026-05-06 04:04:53 +02:00
|
|
|
export const POLL_HEAVY = 60_000;
|
|
|
|
|
export const FETCH_TIMEOUT = 12_000;
|
|
|
|
|
|
|
|
|
|
export function pollDelay({ok = true, base = POLL, failures = 0} = {}) {
|
2026-05-11 17:58:18 +02:00
|
|
|
return ok ? base : Math.min(base * 2 ** Math.min(failures, 4), 300_000);
|
2026-05-06 04:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sleep(ms) {
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 17:58:18 +02:00
|
|
|
// Waits `ms` if the tab is visible; pauses until the tab becomes visible if hidden,
|
|
|
|
|
// then returns immediately so the next poll fires as soon as the user returns.
|
|
|
|
|
export async function waitForVisible(ms) {
|
|
|
|
|
if (typeof document === "undefined") return sleep(ms);
|
|
|
|
|
if (document.visibilityState === "visible") return sleep(ms);
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
const handler = () => {
|
|
|
|
|
document.removeEventListener("visibilitychange", handler);
|
|
|
|
|
resolve();
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener("visibilitychange", handler);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 04:04:53 +02:00
|
|
|
export async function apiFetch(path, options = {}) {
|
|
|
|
|
const url = path.startsWith("http") ? path : `${API}${path}`;
|
|
|
|
|
const timeout = options.timeout ?? FETCH_TIMEOUT;
|
2026-05-19 02:32:22 +02:00
|
|
|
const {timeout: _timeout, ...fetchOptions} = options;
|
2026-05-06 04:04:53 +02:00
|
|
|
const ctrl = new AbortController();
|
|
|
|
|
const timer = setTimeout(() => ctrl.abort(), timeout);
|
|
|
|
|
try {
|
2026-05-19 02:32:22 +02:00
|
|
|
return await fetch(url, {cache: "no-store", ...fetchOptions, signal: ctrl.signal});
|
2026-05-06 04:04:53 +02:00
|
|
|
} finally {
|
|
|
|
|
clearTimeout(timer);
|
|
|
|
|
}
|
|
|
|
|
}
|