fix(sessions): centralize global polling

This commit is contained in:
Bohdan Triapitsyn
2026-08-22 02:49:45 +03:00
parent c232d358da
commit 80068c634d
7 changed files with 88 additions and 30 deletions
@@ -0,0 +1,34 @@
import React from 'react';
import { getAllSyncSessions } from '@/sync/sync-refs';
import {
ensureGlobalSessionsLoaded,
refreshGlobalSessions,
} from '@/stores/useGlobalSessionsStore';
export const GLOBAL_SESSIONS_REFRESH_INTERVAL_MS = 45_000;
type ScheduleInterval = (callback: () => void, delay: number) => number;
type ClearInterval = (intervalId: number) => void;
export const startGlobalSessionsPolling = (
initialLoad: () => void,
refresh: () => void,
scheduleInterval: ScheduleInterval = window.setInterval.bind(window),
clearScheduledInterval: ClearInterval = window.clearInterval.bind(window),
): (() => void) => {
initialLoad();
const intervalId = scheduleInterval(refresh, GLOBAL_SESSIONS_REFRESH_INTERVAL_MS);
return () => clearScheduledInterval(intervalId);
};
/** Owns the one global-session polling lifecycle for the main app runtime. */
export const useGlobalSessionsPolling = (enabled: boolean): void => {
React.useEffect(() => {
if (!enabled) return;
return startGlobalSessionsPolling(
() => { void ensureGlobalSessionsLoaded(getAllSyncSessions()); },
() => { void refreshGlobalSessions(); },
);
}, [enabled]);
};