From 39d0b3c149b6018d42853e8a05593ed5f797d63d Mon Sep 17 00:00:00 2001 From: XLor Date: Sat, 27 May 2023 04:11:01 +0800 Subject: [PATCH] feat: go to search page will abort previous fetch --- packages/app/src/components/Search.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/app/src/components/Search.tsx b/packages/app/src/components/Search.tsx index af25b02a..46f1c598 100644 --- a/packages/app/src/components/Search.tsx +++ b/packages/app/src/components/Search.tsx @@ -60,6 +60,13 @@ export default function Search() { } }, 500); + const signals = useRef>(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; @@ -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; } } ); @@ -123,6 +134,7 @@ export default function Search() { onSelect={() => { if (input) { cleanUp(); + stopFetch(); goToSearch(input); disable(); }