diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-25 09:10:31 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-25 09:10:31 +0200 |
| commit | e5e1482b1f5aea4a50de44801c81c81750042201 (patch) | |
| tree | ad9ed65b20f4a62db3a1d8ad71939e97f6ae9f5d /src/lib/refresh-on-refocus.ts | |
| parent | 2e426068152f97f1615bb9ddc33940a38e7b6861 (diff) | |
Refresh page data on refocus after 10s away, skip edit pages
Diffstat (limited to 'src/lib/refresh-on-refocus.ts')
| -rw-r--r-- | src/lib/refresh-on-refocus.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lib/refresh-on-refocus.ts b/src/lib/refresh-on-refocus.ts new file mode 100644 index 0000000..fcb042a --- /dev/null +++ b/src/lib/refresh-on-refocus.ts @@ -0,0 +1,46 @@ +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); + }; +} |
