Browse Source

fix: a concurrency issue in fetching collections during databases (#779)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 4 months ago
parent
commit
8c286b24ef
2 changed files with 19 additions and 9 deletions
  1. 18 8
      client/src/context/Data.tsx
  2. 1 1
      client/src/pages/home/DatabaseCard.tsx

+ 18 - 8
client/src/context/Data.tsx

@@ -106,6 +106,8 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
   const [databases, setDatabases] = useState<DatabaseObject[]>([]);
   const [databases, setDatabases] = useState<DatabaseObject[]>([]);
   // socket ref
   // socket ref
   const socket = useRef<Socket | null>(null);
   const socket = useRef<Socket | null>(null);
+  // Use a ref to track concurrent requests
+  const requestIdRef = useRef(0);
 
 
   // collection state test
   // collection state test
   const detectLoadingIndexing = useCallback(
   const detectLoadingIndexing = useCallback(
@@ -200,6 +202,8 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
 
 
   // API:fetch collections
   // API:fetch collections
   const fetchCollections = async () => {
   const fetchCollections = async () => {
+    const currentRequestId = ++requestIdRef.current;
+
     try {
     try {
       // set loading true
       // set loading true
       setLoading(true);
       setLoading(true);
@@ -207,14 +211,20 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
       setCollections([]);
       setCollections([]);
       // fetch collections
       // fetch collections
       const res = await CollectionService.getCollections();
       const res = await CollectionService.getCollections();
-      // check state
-      detectLoadingIndexing(res);
-      // set collections
-      setCollections(res);
-      // set loading false
-      setLoading(false);
-    } finally {
-      setLoading(false);
+      // Only process the response if this is the latest request
+      if (currentRequestId === requestIdRef.current) {
+        // check state
+        detectLoadingIndexing(res);
+        // set collections
+        setCollections(res);
+        // set loading false
+        setLoading(false);
+      }
+    } catch (error) {
+      if (currentRequestId === requestIdRef.current) {
+        setLoading(false);
+      }
+      throw error;
     }
     }
   };
   };
 
 

+ 1 - 1
client/src/pages/home/DatabaseCard.tsx

@@ -128,7 +128,7 @@ const DatabaseCard: FC<DatabaseCardProps> = ({
     setDatabase(database.name);
     setDatabase(database.name);
 
 
     // navigate to database detail page
     // navigate to database detail page
-    const targetPath = `/databases/${database.name}/colletions`;
+    const targetPath = `/databases/${database.name}/collections`;
 
 
     navigate(targetPath);
     navigate(targetPath);
   };
   };