Skip to content

Commit

Permalink
Merge pull request #1326 from hydralauncher/fix/adding-debounce-to-se…
Browse files Browse the repository at this point in the history
…arch

fix: adding debounce to search
  • Loading branch information
zamitto authored Dec 24, 2024
2 parents e50cb74 + 36800c6 commit 3613ad1
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/renderer/src/pages/catalogue/catalogue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Pagination } from "./pagination";
import { useCatalogue } from "@renderer/hooks/use-catalogue";
import { GameItem } from "./game-item";
import { FilterItem } from "./filter-item";
import { debounce } from "lodash-es";

const filterCategoryColors = {
genres: "hsl(262deg 50% 47%)",
Expand Down Expand Up @@ -58,26 +59,36 @@ export default function Catalogue() {

const { getRepacksForObjectId } = useRepacks();

const debouncedSearch = useRef(
debounce(async (filters, pageSize, offset) => {
const abortController = new AbortController();
abortControllerRef.current = abortController;

const response = await window.electron.searchGames(
filters,
pageSize,
offset
);

if (abortController.signal.aborted) return;

setResults(response.edges);
setItemsCount(response.count);
setIsLoading(false);
}, 500)
).current;

useEffect(() => {
setResults([]);
setIsLoading(true);
abortControllerRef.current?.abort();

const abortController = new AbortController();
abortControllerRef.current = abortController;

window.electron
.searchGames(filters, PAGE_SIZE, (page - 1) * PAGE_SIZE)
.then((response) => {
if (abortController.signal.aborted) {
return;
}

setResults(response.edges);
setItemsCount(response.count);
setIsLoading(false);
});
}, [filters, page, dispatch]);
debouncedSearch(filters, PAGE_SIZE, (page - 1) * PAGE_SIZE);

return () => {
debouncedSearch.cancel();
};
}, [filters, page, debouncedSearch]);

useEffect(() => {
downloadSourcesTable.toArray().then((sources) => {
Expand Down

0 comments on commit 3613ad1

Please sign in to comment.