aboutsummaryrefslogtreecommitdiff
path: root/src/lib/refresh-on-refocus.ts
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-10-18 21:26:24 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-10-18 21:26:24 +0200
commit031f99e626f52ea7baac9155b0cbe6e4fcfbfb31 (patch)
tree5c6851213527ce7c8f5945c56a11448c7e8fdeac /src/lib/refresh-on-refocus.ts
parent66939bab93cdcb9b7732c45d7bc3b99b6951fe15 (diff)
feat: poll server data so shared pages stay near-live
Refetch the current page every 15s while the tab is visible, gated by a pure refresh policy: edit pages, hidden or offline tabs and in-flight navigations are left alone. Replaces refresh-on-refocus with auto-refresh.
Diffstat (limited to 'src/lib/refresh-on-refocus.ts')
-rw-r--r--src/lib/refresh-on-refocus.ts46
1 files changed, 0 insertions, 46 deletions
diff --git a/src/lib/refresh-on-refocus.ts b/src/lib/refresh-on-refocus.ts
deleted file mode 100644
index fcb042a..0000000
--- a/src/lib/refresh-on-refocus.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { invalidateAll } from '$app/navigation';
-import { page } from '$app/stores';
-import { get } from 'svelte/store';
-
-// Skip the refocus refresh on login and edit pages (in-progress edits).
-const EXCLUDED_ROUTES: Record<string, true> = {
- '/login': true,
- '/grocery/[id]': true,
- '/projects/[projectId]/routine/[routineId]': true,
-};
-
-const MIN_AWAY_MS = 10_000;
-
-/** Re-run page loads on refocus after MIN_AWAY_MS away; call from onMount. */
-export function refreshOnRefocus(): () => void {
- let deactivatedAt: number | null = null;
-
- const deactivate = () => {
- deactivatedAt ??= Date.now();
- };
-
- const activate = () => {
- if (deactivatedAt === null || document.visibilityState !== 'visible')
- return;
- const awayMs = Date.now() - deactivatedAt;
- deactivatedAt = null;
- if (awayMs < MIN_AWAY_MS) return;
- if ((get(page).route.id ?? '') in EXCLUDED_ROUTES) return;
- invalidateAll();
- };
-
- const onVisibilityChange = () => {
- if (document.visibilityState === 'hidden') deactivate();
- else activate();
- };
-
- document.addEventListener('visibilitychange', onVisibilityChange);
- window.addEventListener('blur', deactivate);
- window.addEventListener('focus', activate);
-
- return () => {
- document.removeEventListener('visibilitychange', onVisibilityChange);
- window.removeEventListener('blur', deactivate);
- window.removeEventListener('focus', activate);
- };
-}