[mob] Clean up code

This commit is contained in:
Neeraj Gupta 2024-04-11 15:31:11 +05:30
parent 1cc188dd9c
commit ff4e64819b

View file

@ -49,9 +49,10 @@ class SemanticSearchService {
bool _hasInitialized = false;
bool _isComputingEmbeddings = false;
bool _isSyncing = false;
Future<List<EnteFile>>? _ongoingRequest;
List<Embedding> _cachedEmbeddings = <Embedding>[];
PendingQuery? _nextQuery;
Future<(String, List<EnteFile>)>? _searchScreenRequest;
String? _latestPendingQuery;
Completer<void> _mlController = Completer<void>();
get hasInitialized => _hasInitialized;
@ -133,9 +134,6 @@ class SemanticSearchService {
_isSyncing = false;
}
Future<(String, List<EnteFile>)>? _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<EnteFile>)> 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<List<EnteFile>> 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<List<EnteFile>>());
return _nextQuery!.completer.future;
}
}
Future<IndexStatus> getIndexStatus() async {
return IndexStatus(
_cachedEmbeddings.length,
@ -467,13 +435,6 @@ class QueryResult {
QueryResult(this.id, this.score);
}
class PendingQuery {
final String query;
final Completer<List<EnteFile>> completer;
PendingQuery(this.query, this.completer);
}
class IndexStatus {
final int indexedItems, pendingItems;