From c44aab369c4a1ce3f3c3389c86bef34790e0845b Mon Sep 17 00:00:00 2001 From: martincupela Date: Fri, 20 Dec 2024 17:46:24 +0100 Subject: [PATCH] fix: prevent infinite loop in search --- src/experimental/Search/SearchController.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/experimental/Search/SearchController.ts b/src/experimental/Search/SearchController.ts index c464f4d14..5d9d2151b 100644 --- a/src/experimental/Search/SearchController.ts +++ b/src/experimental/Search/SearchController.ts @@ -141,7 +141,7 @@ export type SearchSourceOptions = { }; const DEFAULT_SEARCH_SOURCE_OPTIONS: Required = { - debounceMs: 5000, + debounceMs: 300, isActive: false, pageSize: 10, } as const; @@ -224,19 +224,21 @@ export abstract class BaseSearchSource implements SearchSource { this.state.partialNext({ isActive: false }); }; - async executeQuery(searchQuery: string) { - const hasNewSearchQuery = typeof searchQuery !== 'undefined'; - if (!this.isActive || this.isLoading || !this.hasMore || !searchQuery) return; + async executeQuery(newSearchString?: string) { + const hasNewSearchQuery = typeof newSearchString !== 'undefined'; + const searchString = newSearchString ?? this.searchQuery; + if (!this.isActive || this.isLoading || (!this.hasMore && !hasNewSearchQuery) || !searchString) + return; if (hasNewSearchQuery) { - this.resetState({ isActive: this.isActive, isLoading: true, searchQuery }); + this.resetState({ isActive: this.isActive, isLoading: true, searchQuery: newSearchString }); } else { this.state.partialNext({ isLoading: true }); } const stateUpdate: Partial> = {}; try { - const results = await this.query(searchQuery); + const results = await this.query(searchString); if (!results) return; const { items, next } = results; @@ -265,7 +267,7 @@ export abstract class BaseSearchSource implements SearchSource { search = async (searchQuery?: string) => { await new Promise((resolve) => { this.resolveDebouncedSearch = resolve; - this.searchDebounced(searchQuery ?? this.searchQuery); + this.searchDebounced(searchQuery); }); };