Add more Live Event refreshing (#9745)
* Refresh on foreground
* Add back interval
(cherry picked from commit 3b7806963a)
This commit is contained in:
@@ -51,7 +51,7 @@ export class MetricsClient<M extends Record<string, any>> {
|
|||||||
}
|
}
|
||||||
this.queue.push(e)
|
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) {
|
if (this.queue.length > this.maxBatchSize) {
|
||||||
this.flush()
|
this.flush()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {createContext, useContext, useMemo} from 'react'
|
import {createContext, useContext, useMemo} from 'react'
|
||||||
import {QueryClient, useQuery} from '@tanstack/react-query'
|
import {QueryClient, useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {useOnAppStateChange} from '#/lib/appState'
|
||||||
import {useIsBskyTeam} from '#/lib/hooks/useIsBskyTeam'
|
import {useIsBskyTeam} from '#/lib/hooks/useIsBskyTeam'
|
||||||
import {
|
import {
|
||||||
convertBskyAppUrlIfNeeded,
|
convertBskyAppUrlIfNeeded,
|
||||||
@@ -35,12 +36,12 @@ const Context = createContext<LiveEventsWorkerResponse>(DEFAULT_LIVE_EVENTS)
|
|||||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
const [isDevMode] = useDevMode()
|
const [isDevMode] = useDevMode()
|
||||||
const isBskyTeam = useIsBskyTeam()
|
const isBskyTeam = useIsBskyTeam()
|
||||||
const {data} = useQuery(
|
const {data, refetch} = useQuery(
|
||||||
{
|
{
|
||||||
// keep this, prefectching handles initial load
|
// keep this, prefectching handles initial load
|
||||||
staleTime: 1000 * 15,
|
staleTime: 1000 * 15,
|
||||||
queryKey: liveEventsQueryKey,
|
queryKey: liveEventsQueryKey,
|
||||||
refetchInterval: 1000 * 60 * 5,
|
refetchInterval: 1000 * 60 * 5, // refetch every 5 minutes
|
||||||
async queryFn() {
|
async queryFn() {
|
||||||
return fetchLiveEvents()
|
return fetchLiveEvents()
|
||||||
},
|
},
|
||||||
@@ -48,6 +49,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
qc,
|
qc,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
useOnAppStateChange(state => {
|
||||||
|
if (state === 'active') refetch()
|
||||||
|
})
|
||||||
|
|
||||||
const ctx = useMemo(() => {
|
const ctx = useMemo(() => {
|
||||||
if (!data) return DEFAULT_LIVE_EVENTS
|
if (!data) return DEFAULT_LIVE_EVENTS
|
||||||
const feeds = data.feeds.filter(f => {
|
const feeds = data.feeds.filter(f => {
|
||||||
|
|||||||
+8
-8
@@ -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() {
|
export function useAppState() {
|
||||||
const [state, setState] = useState(AppState.currentState)
|
const [state, setState] = useState(AppState.currentState)
|
||||||
|
useOnAppStateChange(setState)
|
||||||
useEffect(() => {
|
|
||||||
const sub = onAppStateChange(next => {
|
|
||||||
setState(next)
|
|
||||||
})
|
|
||||||
return () => sub.remove()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user