23 lines
889 B
TypeScript
23 lines
889 B
TypeScript
|
|
import { useQuery, type QueryKey } from "@tanstack/react-query";
|
||
|
|
|
||
|
|
// Reusable "live" polling query: a thin wrapper over react-query that refetches on an
|
||
|
|
// interval and — by leaving refetchIntervalInBackground at its default false — pauses while
|
||
|
|
// the tab is unfocused, so it doesn't poll a server nobody's watching. This is the shared
|
||
|
|
// live-progress mechanism: the Scheduler dashboard uses it now; the notification bell and the
|
||
|
|
// future yt-dlp job queue reuse it rather than each re-implementing polling.
|
||
|
|
export function useLiveQuery<T>(
|
||
|
|
key: QueryKey,
|
||
|
|
queryFn: () => Promise<T>,
|
||
|
|
opts: { intervalMs?: number; enabled?: boolean } = {}
|
||
|
|
) {
|
||
|
|
const { intervalMs = 4000, enabled = true } = opts;
|
||
|
|
return useQuery({
|
||
|
|
queryKey: key,
|
||
|
|
queryFn,
|
||
|
|
enabled,
|
||
|
|
refetchInterval: enabled ? intervalMs : false,
|
||
|
|
refetchIntervalInBackground: false,
|
||
|
|
staleTime: 0,
|
||
|
|
});
|
||
|
|
}
|