From 1317d881ed2f583a65bffeb835b2d57670cce235 Mon Sep 17 00:00:00 2001
From: Samuel Newman <mozzius@protonmail.com>
Date: Mon, 10 Jun 2024 15:08:13 +0100
Subject: debounce refetching listconvos (#4455)

---
 src/state/queries/messages/list-converations.tsx | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

(limited to 'src/state/queries/messages/list-converations.tsx')

diff --git a/src/state/queries/messages/list-converations.tsx b/src/state/queries/messages/list-converations.tsx
index ce2cd7079..306d4cae5 100644
--- a/src/state/queries/messages/list-converations.tsx
+++ b/src/state/queries/messages/list-converations.tsx
@@ -16,6 +16,7 @@ import {
   useInfiniteQuery,
   useQueryClient,
 } from '@tanstack/react-query'
+import debounce from 'lodash.debounce'
 
 import {useCurrentConvoId} from '#/state/messages/current-convo-id'
 import {useMessagesEventBus} from '#/state/messages/events'
@@ -89,6 +90,11 @@ export function ListConvosProviderInner({
   const {currentConvoId} = useCurrentConvoId()
   const {currentAccount} = useSession()
 
+  const debouncedRefetch = useMemo(
+    () => debounce(() => refetch, 500),
+    [refetch],
+  )
+
   useEffect(() => {
     const unsub = messagesBus.on(
       events => {
@@ -96,7 +102,7 @@ export function ListConvosProviderInner({
 
         events.logs.forEach(log => {
           if (ChatBskyConvoDefs.isLogBeginConvo(log)) {
-            refetch()
+            debouncedRefetch()
           } else if (ChatBskyConvoDefs.isLogLeaveConvo(log)) {
             queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) =>
               optimisticDelete(log.convoId, old),
@@ -170,7 +176,7 @@ export function ListConvosProviderInner({
                   }),
                 }
               } else {
-                refetch()
+                debouncedRefetch()
               }
             })
           }
@@ -183,7 +189,14 @@ export function ListConvosProviderInner({
     )
 
     return () => unsub()
-  }, [messagesBus, currentConvoId, refetch, queryClient, currentAccount?.did])
+  }, [
+    messagesBus,
+    currentConvoId,
+    refetch,
+    queryClient,
+    currentAccount?.did,
+    debouncedRefetch,
+  ])
 
   const ctx = useMemo(() => {
     return data?.pages.flatMap(page => page.convos) ?? []
-- 
cgit 1.4.1

