97 lines
3.0 KiB
JavaScript
97 lines
3.0 KiB
JavaScript
const DEFAULT_POLL_INTERVAL_MS = 1000;
|
|
const DEFAULT_WAIT_TIMEOUT_MS = 135_000;
|
|
|
|
function waitForPoll(delayMs, signal) {
|
|
return new Promise((resolve, reject) => {
|
|
if (signal.aborted) {
|
|
reject(signal.reason);
|
|
return;
|
|
}
|
|
const timer = setTimeout(() => {
|
|
signal.removeEventListener("abort", abort);
|
|
resolve();
|
|
}, delayMs);
|
|
const abort = () => {
|
|
clearTimeout(timer);
|
|
reject(signal.reason);
|
|
};
|
|
signal.addEventListener("abort", abort, { once: true });
|
|
});
|
|
}
|
|
|
|
export function createNewsRefreshController({
|
|
submit,
|
|
getStatus,
|
|
loadLatest,
|
|
onBusyChange,
|
|
onSuccess,
|
|
onFailure,
|
|
onCancelled = () => {},
|
|
now = Date.now,
|
|
wait = waitForPoll,
|
|
pollIntervalMs = DEFAULT_POLL_INTERVAL_MS,
|
|
waitTimeoutMs = DEFAULT_WAIT_TIMEOUT_MS,
|
|
}) {
|
|
let active = null;
|
|
|
|
function cancel(reason = "navigation") {
|
|
if (!active) return;
|
|
const run = active;
|
|
active = null;
|
|
run.controller.abort();
|
|
onBusyChange(false);
|
|
if (reason === "view_changed") onCancelled(reason);
|
|
}
|
|
|
|
function start(context) {
|
|
if (active) return active.promise;
|
|
const run = { controller: new AbortController(), promise: null, context };
|
|
active = run;
|
|
onBusyChange(true);
|
|
run.promise = (async () => {
|
|
const signal = run.controller.signal;
|
|
const deadline = now() + waitTimeoutMs;
|
|
try {
|
|
const accepted = await submit(context, signal);
|
|
if (signal.aborted || active !== run) return;
|
|
if (!accepted?.refresh_id) throw new Error("missing_refresh_id");
|
|
let status = accepted;
|
|
while (status.status === "pending" || status.status === "running") {
|
|
if (now() >= deadline) throw Object.assign(new Error("refresh_timeout"), { code: "timeout" });
|
|
await wait(pollIntervalMs, signal);
|
|
if (now() >= deadline) throw Object.assign(new Error("refresh_timeout"), { code: "timeout" });
|
|
status = await getStatus(accepted.refresh_id, signal);
|
|
if (signal.aborted || active !== run) return;
|
|
if (now() >= deadline && status.status !== "failed") {
|
|
throw Object.assign(new Error("refresh_timeout"), { code: "timeout" });
|
|
}
|
|
}
|
|
if (status.status === "failed") {
|
|
throw Object.assign(new Error(status.error?.message || "refresh_failed"), {
|
|
code: status.error?.code || "refresh_failed",
|
|
});
|
|
}
|
|
if (status.status !== "succeeded") throw new Error("invalid_refresh_status");
|
|
if (signal.aborted || active !== run) return;
|
|
await loadLatest(context, signal);
|
|
if (!signal.aborted && active === run) onSuccess();
|
|
} catch (error) {
|
|
if (!signal.aborted && active === run) onFailure(error);
|
|
} finally {
|
|
if (active === run) {
|
|
active = null;
|
|
onBusyChange(false);
|
|
}
|
|
}
|
|
})();
|
|
return run.promise;
|
|
}
|
|
|
|
return {
|
|
start,
|
|
cancel,
|
|
isBusy: () => active !== null,
|
|
activeContext: () => active?.context || null,
|
|
};
|
|
}
|