generated from coulomb/repo-seed
feat(dashboard): poll optimisation — T4, T5, T6
T4: workstreams.md and dependencies.md now call /state/deps instead of the
full /state/summary — removes 2 heavy 10-table queries per 60 s cycle.
T5: index.md's 4 independent polling loops (summaryState, sbomSnapState,
regsState, wsChartState) consolidated into a single pageState generator
with one Promise.all batch and a shared backoff counter.
T6: config.js gains waitForVisible(ms) — pauses polling entirely while the
tab is hidden and fires immediately on visibilitychange. pollDelay()
simplified (hidden-tab POLL_HIDDEN logic removed). All 16 polling pages
migrated from await sleep(pollDelay(...)) to await waitForVisible(pollDelay(...)).
CUST-WP-0039 complete — all 6 tasks done.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
export const API = "http://127.0.0.1:8000";
|
||||
export const POLL = 15_000;
|
||||
export const POLL_HEAVY = 60_000;
|
||||
export const POLL_HIDDEN = 120_000;
|
||||
export const FETCH_TIMEOUT = 12_000;
|
||||
|
||||
export function pollDelay({ok = true, base = POLL, failures = 0} = {}) {
|
||||
const hidden = typeof document !== "undefined" && document.visibilityState === "hidden";
|
||||
const failureDelay = ok ? base : Math.min(base * 2 ** Math.min(failures, 4), 300_000);
|
||||
return hidden ? Math.max(failureDelay, POLL_HIDDEN) : failureDelay;
|
||||
return ok ? base : Math.min(base * 2 ** Math.min(failures, 4), 300_000);
|
||||
}
|
||||
|
||||
export function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
export async function apiFetch(path, options = {}) {
|
||||
const url = path.startsWith("http") ? path : `${API}${path}`;
|
||||
const timeout = options.timeout ?? FETCH_TIMEOUT;
|
||||
|
||||
Reference in New Issue
Block a user