Maintain some feed data to avoid needless glimmers (#2054)

This commit is contained in:
Paul Frazee 2023-11-30 18:49:23 -08:00 committed by GitHub
parent 9fa90bb8d9
commit 826cbbd4bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 40 additions and 23 deletions

View file

@ -14,6 +14,7 @@ import {isNative} from '#/platform/detection'
import {useMutedThreads} from '#/state/muted-threads'
import {RQKEY as RQKEY_NOTIFS} from './feed'
import {logger} from '#/logger'
import {truncateAndInvalidate} from '../util'
const UPDATE_INTERVAL = 30 * 1e3 // 30sec
@ -126,7 +127,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
// update & broadcast
setNumUnread(unreadCountStr)
if (invalidate) {
queryClient.resetQueries({queryKey: RQKEY_NOTIFS()})
truncateAndInvalidate(queryClient, RQKEY_NOTIFS())
}
broadcast.postMessage({event: unreadCountStr})
} catch (e) {

17
src/state/queries/util.ts Normal file
View file

@ -0,0 +1,17 @@
import {QueryClient, QueryKey, InfiniteData} from '@tanstack/react-query'
export function truncateAndInvalidate<T = any>(
queryClient: QueryClient,
querykey: QueryKey,
) {
queryClient.setQueryData<InfiniteData<T>>(querykey, data => {
if (data) {
return {
pageParams: data.pageParams.slice(0, 1),
pages: data.pages.slice(0, 1),
}
}
return data
})
queryClient.invalidateQueries({queryKey: querykey})
}