1
0
forked from wrenn/wrenn

Optimize frontend polling: visibility API, range-based intervals, skip redundant redraws

Adds Page Visibility API to StatsPanel, templates, and capsule detail
pages so polling pauses when the browser tab is hidden. Capsule metrics
now use range-appropriate poll intervals (10s for 5m/10m, up to 120s for
24h) instead of a flat 10s. Chart updates are skipped when the data
fingerprint hasn't changed, avoiding unnecessary Canvas redraws.
This commit is contained in:
2026-04-11 06:20:29 +06:00
parent dbad418093
commit 21b82c2283
4 changed files with 77 additions and 10 deletions

View File

@ -36,6 +36,7 @@
// Polling
let pollInterval: ReturnType<typeof setInterval> | null = null;
let hasActiveBuilds = $derived(builds.some((b) => b.status === 'pending' || b.status === 'running'));
let visibilityHandler: (() => void) | null = null;
// Build log expansion
let expandedBuildId = $state<string | null>(null);
@ -97,7 +98,7 @@
function startPolling() {
stopPolling();
pollInterval = setInterval(() => {
if (hasActiveBuilds) fetchBuilds();
if (hasActiveBuilds && activeTab === 'builds') fetchBuilds();
}, 3000);
}
@ -242,9 +243,22 @@
onMount(() => {
fetchTemplates();
fetchBuilds().then(startPolling);
// Pause polling when the browser tab is hidden.
visibilityHandler = () => {
if (document.hidden) {
stopPolling();
} else {
startPolling();
}
};
document.addEventListener('visibilitychange', visibilityHandler);
});
onDestroy(stopPolling);
onDestroy(() => {
stopPolling();
if (visibilityHandler) document.removeEventListener('visibilitychange', visibilityHandler);
});
</script>
<div class="flex h-screen overflow-hidden bg-[var(--color-bg-0)]">