Skip to content

Commit

Permalink
cancel search when global search is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Soopyboo32 committed Sep 25, 2024
1 parent b5658e1 commit a6703c1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/screens/GlobalSearchScreen/hooks/useGlobalSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export interface GlobalSearchResult {

export const useGlobalSearch = ({ defaultSearchText }: Props) => {
const isMounted = useRef(true);
useEffect(
() => () => {
isMounted.current = false;
},
[],
);

const { filteredInstalledPlugins } = usePlugins();

Expand All @@ -40,7 +46,6 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {
let running = 0;

async function searchInPlugin(_plugin: PluginItem) {
running++;
try {
const plugin = getPlugin(_plugin.id);
if (!plugin) {
Expand Down Expand Up @@ -91,7 +96,6 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {
setProgress(
prevState => prevState + 1 / filteredInstalledPlugins.length,
);
running--;
}
}

Expand All @@ -102,13 +106,20 @@ export const useGlobalSearch = ({ defaultSearchText }: Props) => {

(async () => {
for (let _plugin of filteredSortedInstalledPlugins) {
if (!isMounted.current) {
break;
}
while (running >= globalSearchConcurrency) {
await new Promise(resolve => setTimeout(resolve, 100));
}
searchInPlugin(_plugin).then();
if (!isMounted.current) {
break;
}
running++;
searchInPlugin(_plugin)
.then(() => {
running--;
})
.catch(() => {
running--;
});
}
})();
};
Expand Down

0 comments on commit a6703c1

Please sign in to comment.