Skip to content

Commit

Permalink
feat: go to search page will abort previous fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed May 26, 2023
1 parent 614f082 commit 39d0b3c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/app/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export default function Search() {
}
}, 500);

const signals = useRef<Set<AbortController>>(new Set());
const stopFetch = useCallback(() => {
for (const abort of signals.current) {
abort.abort();
}
signals.current.clear();
}, []);
const { data: searchResult, isLoading } = useSWR(
() => {
if (!search) return null;
Expand All @@ -71,7 +78,11 @@ export default function Search() {
if (DMHY_RE.test(search)) {
return [];
} else {
return await fetchResources(1, { ...parseSearch(search) });
const abort = new AbortController();
signals.current.add(abort);
const res = await fetchResources(1, { ...parseSearch(search), signal: abort.signal });
signals.current.delete(abort);
return res;
}
}
);
Expand Down Expand Up @@ -123,6 +134,7 @@ export default function Search() {
onSelect={() => {
if (input) {
cleanUp();
stopFetch();
goToSearch(input);
disable();
}
Expand Down

0 comments on commit 39d0b3c

Please sign in to comment.