debounce refetching listconvos (#4455)
parent
01b7a94a7e
commit
1317d881ed
|
@ -16,6 +16,7 @@ import {
|
||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
useQueryClient,
|
useQueryClient,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
import debounce from 'lodash.debounce'
|
||||||
|
|
||||||
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
|
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
|
||||||
import {useMessagesEventBus} from '#/state/messages/events'
|
import {useMessagesEventBus} from '#/state/messages/events'
|
||||||
|
@ -89,6 +90,11 @@ export function ListConvosProviderInner({
|
||||||
const {currentConvoId} = useCurrentConvoId()
|
const {currentConvoId} = useCurrentConvoId()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
|
||||||
|
const debouncedRefetch = useMemo(
|
||||||
|
() => debounce(() => refetch, 500),
|
||||||
|
[refetch],
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsub = messagesBus.on(
|
const unsub = messagesBus.on(
|
||||||
events => {
|
events => {
|
||||||
|
@ -96,7 +102,7 @@ export function ListConvosProviderInner({
|
||||||
|
|
||||||
events.logs.forEach(log => {
|
events.logs.forEach(log => {
|
||||||
if (ChatBskyConvoDefs.isLogBeginConvo(log)) {
|
if (ChatBskyConvoDefs.isLogBeginConvo(log)) {
|
||||||
refetch()
|
debouncedRefetch()
|
||||||
} else if (ChatBskyConvoDefs.isLogLeaveConvo(log)) {
|
} else if (ChatBskyConvoDefs.isLogLeaveConvo(log)) {
|
||||||
queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) =>
|
queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) =>
|
||||||
optimisticDelete(log.convoId, old),
|
optimisticDelete(log.convoId, old),
|
||||||
|
@ -170,7 +176,7 @@ export function ListConvosProviderInner({
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
refetch()
|
debouncedRefetch()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -183,7 +189,14 @@ export function ListConvosProviderInner({
|
||||||
)
|
)
|
||||||
|
|
||||||
return () => unsub()
|
return () => unsub()
|
||||||
}, [messagesBus, currentConvoId, refetch, queryClient, currentAccount?.did])
|
}, [
|
||||||
|
messagesBus,
|
||||||
|
currentConvoId,
|
||||||
|
refetch,
|
||||||
|
queryClient,
|
||||||
|
currentAccount?.did,
|
||||||
|
debouncedRefetch,
|
||||||
|
])
|
||||||
|
|
||||||
const ctx = useMemo(() => {
|
const ctx = useMemo(() => {
|
||||||
return data?.pages.flatMap(page => page.convos) ?? []
|
return data?.pages.flatMap(page => page.convos) ?? []
|
||||||
|
|
Loading…
Reference in New Issue