From 15b26b25a7899e5508596d88a30b5000af7c963d Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Sat, 24 Jan 2026 13:39:55 -0600 Subject: [PATCH] Add more Live Event refreshing (#9745) * Refresh on foreground * Add back interval (cherry picked from commit 3b7806963a4fb15cd739f954f9797655b16c03b1) --- src/analytics/metrics/client.ts | 2 +- src/features/liveEvents/context.tsx | 9 +++++++-- src/lib/appState.ts | 16 ++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/analytics/metrics/client.ts b/src/analytics/metrics/client.ts index 8ed02f3804..868cbc93e6 100644 --- a/src/analytics/metrics/client.ts +++ b/src/analytics/metrics/client.ts @@ -51,7 +51,7 @@ export class MetricsClient> { } this.queue.push(e) - logger.info(`event: ${e.event as string}`, e) + logger.debug(`event: ${e.event as string}`, e) if (this.queue.length > this.maxBatchSize) { this.flush() diff --git a/src/features/liveEvents/context.tsx b/src/features/liveEvents/context.tsx index 97f3cd3e5e..b39195539a 100644 --- a/src/features/liveEvents/context.tsx +++ b/src/features/liveEvents/context.tsx @@ -1,6 +1,7 @@ import {createContext, useContext, useMemo} from 'react' import {QueryClient, useQuery} from '@tanstack/react-query' +import {useOnAppStateChange} from '#/lib/appState' import {useIsBskyTeam} from '#/lib/hooks/useIsBskyTeam' import { convertBskyAppUrlIfNeeded, @@ -35,12 +36,12 @@ const Context = createContext(DEFAULT_LIVE_EVENTS) export function Provider({children}: React.PropsWithChildren<{}>) { const [isDevMode] = useDevMode() const isBskyTeam = useIsBskyTeam() - const {data} = useQuery( + const {data, refetch} = useQuery( { // keep this, prefectching handles initial load staleTime: 1000 * 15, queryKey: liveEventsQueryKey, - refetchInterval: 1000 * 60 * 5, + refetchInterval: 1000 * 60 * 5, // refetch every 5 minutes async queryFn() { return fetchLiveEvents() }, @@ -48,6 +49,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) { qc, ) + useOnAppStateChange(state => { + if (state === 'active') refetch() + }) + const ctx = useMemo(() => { if (!data) return DEFAULT_LIVE_EVENTS const feeds = data.feeds.filter(f => { diff --git a/src/lib/appState.ts b/src/lib/appState.ts index 042e2d480d..6cc2337129 100644 --- a/src/lib/appState.ts +++ b/src/lib/appState.ts @@ -12,15 +12,15 @@ export function onAppStateChange(cb: (state: AppStateStatus) => void) { }) } +export function useOnAppStateChange(cb: (state: AppStateStatus) => void) { + useEffect(() => { + const sub = onAppStateChange(next => cb(next)) + return () => sub.remove() + }, [cb]) +} + export function useAppState() { const [state, setState] = useState(AppState.currentState) - - useEffect(() => { - const sub = onAppStateChange(next => { - setState(next) - }) - return () => sub.remove() - }, []) - + useOnAppStateChange(setState) return state }