From ff4e64819be45943815f6b58eed8c85ea2a26c6d Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Thu, 11 Apr 2024 15:31:11 +0530 Subject: [PATCH] [mob] Clean up code --- .../semantic_search_service.dart | 57 +++---------------- 1 file changed, 9 insertions(+), 48 deletions(-) diff --git a/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart b/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart index 192e6d580..d1074053a 100644 --- a/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart +++ b/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart @@ -49,9 +49,10 @@ class SemanticSearchService { bool _hasInitialized = false; bool _isComputingEmbeddings = false; bool _isSyncing = false; - Future>? _ongoingRequest; List _cachedEmbeddings = []; - PendingQuery? _nextQuery; + Future<(String, List)>? _searchScreenRequest; + String? _latestPendingQuery; + Completer _mlController = Completer(); get hasInitialized => _hasInitialized; @@ -133,9 +134,6 @@ class SemanticSearchService { _isSyncing = false; } - Future<(String, List)>? _searchScreenRequest; - String? _lastQuery; - // searchScreenQuery should only be used for the user initiate query on the search screen. // If there are multiple call tho this method, then for all the calls, the result will be the same as the last query. Future<(String, List)> searchScreenQuery(String query) async { @@ -145,7 +143,7 @@ class SemanticSearchService { } // If there's an ongoing request, just update the last query and return its future. if (_searchScreenRequest != null) { - _lastQuery = query; + _latestPendingQuery = query; return _searchScreenRequest!; } else { // No ongoing request, start a new search. @@ -153,12 +151,11 @@ class SemanticSearchService { // Search completed, reset the ongoing request. _searchScreenRequest = null; // If there was a new query during the last search, start a new search with the last query. - if (_lastQuery != null) { - final String newQuery = _lastQuery!; - _lastQuery = null; // Reset last query. - return searchScreenQuery( - newQuery, - ); // Recursively call search with the latest query. + if (_latestPendingQuery != null) { + final String newQuery = _latestPendingQuery!; + _latestPendingQuery = null; // Reset last query. + // Recursively call search with the latest query. + return searchScreenQuery(newQuery); } return (query, result); }); @@ -166,35 +163,6 @@ class SemanticSearchService { } } - Future> search(String query) async { - if (!LocalSettings.instance.hasEnabledMagicSearch() || - !_frameworkInitialization.isCompleted) { - return []; - } - if (_ongoingRequest == null) { - _ongoingRequest = _getMatchingFiles(query).then((result) { - _ongoingRequest = null; - if (_nextQuery != null) { - final next = _nextQuery; - _nextQuery = null; - search(next!.query).then((nextResult) { - next.completer.complete(nextResult); - }); - } - - return result; - }); - return _ongoingRequest!; - } else { - // If there's an ongoing request, create or replace the nextCompleter. - _logger.info("Queuing query $query"); - await _nextQuery?.completer.future - .timeout(const Duration(seconds: 0)); // Cancels the previous future. - _nextQuery = PendingQuery(query, Completer>()); - return _nextQuery!.completer.future; - } - } - Future getIndexStatus() async { return IndexStatus( _cachedEmbeddings.length, @@ -467,13 +435,6 @@ class QueryResult { QueryResult(this.id, this.score); } -class PendingQuery { - final String query; - final Completer> completer; - - PendingQuery(this.query, this.completer); -} - class IndexStatus { final int indexedItems, pendingItems;