Poll for new posts on app foreground (#2152)

zio/stable
Paul Frazee 2023-12-08 16:30:19 -08:00 committed by GitHub
parent 9c0c18d5d0
commit 102094b10a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import React, {memo, MutableRefObject} from 'react' import React, {memo, MutableRefObject} from 'react'
import { import {
ActivityIndicator, ActivityIndicator,
AppState,
Dimensions, Dimensions,
RefreshControl, RefreshControl,
StyleProp, StyleProp,
@ -142,12 +143,23 @@ let Feed = ({
} }
}, [enabled]) }, [enabled])
React.useEffect(() => { React.useEffect(() => {
if (!pollInterval) { let cleanup1: () => void | undefined, cleanup2: () => void | undefined
return const subscription = AppState.addEventListener('change', nextAppState => {
// check for new on app foreground
if (nextAppState === 'active') {
checkForNewRef.current?.()
} }
})
cleanup1 = () => subscription.remove()
if (pollInterval) {
// check for new on interval // check for new on interval
const i = setInterval(() => checkForNewRef.current?.(), pollInterval) const i = setInterval(() => checkForNewRef.current?.(), pollInterval)
return () => clearInterval(i) cleanup2 = () => clearInterval(i)
}
return () => {
cleanup1?.()
cleanup2?.()
}
}, [pollInterval]) }, [pollInterval])
const feedItems = React.useMemo(() => { const feedItems = React.useMemo(() => {