Skip to content

Commit

Permalink
fix: a concurrency issue in fetching collections during databases (#779)
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid authored Feb 25, 2025
1 parent 782d12c commit 8c286b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions client/src/context/Data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
const [databases, setDatabases] = useState<DatabaseObject[]>([]);
// socket ref
const socket = useRef<Socket | null>(null);
// Use a ref to track concurrent requests
const requestIdRef = useRef(0);

// collection state test
const detectLoadingIndexing = useCallback(
Expand Down Expand Up @@ -200,21 +202,29 @@ export const DataProvider = (props: { children: React.ReactNode }) => {

// API:fetch collections
const fetchCollections = async () => {
const currentRequestId = ++requestIdRef.current;

try {
// set loading true
setLoading(true);
// set collections
setCollections([]);
// fetch collections
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;
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/home/DatabaseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const DatabaseCard: FC<DatabaseCardProps> = ({
setDatabase(database.name);

// navigate to database detail page
const targetPath = `/databases/${database.name}/colletions`;
const targetPath = `/databases/${database.name}/collections`;

navigate(targetPath);
};
Expand Down

0 comments on commit 8c286b2

Please sign in to comment.