search method

void search(
  1. String query
)

Search function that combines instant local and debounced remote search

Implementation

void search(String query) {
  _currentQuery = query;

  // Cancel previous timer if exists
  if (_debounceTimer?.isActive ?? false) {
    _debounceTimer!.cancel();
  }

  // IMMEDIATELY perform local search - no loading state
  List<T> localResults = _searchLocalData(query);

  // Always emit local results immediately
  _searchResultsController.add(SearchResult<T>(
    items: localResults,
    isLoading: false,
    query: query,
    source: SearchSource.local,
  ));

  // If local search found results, don't trigger remote search
  if (localResults.isNotEmpty) {
    return;
  }

  // Only if local search returned no results and query isn't empty, schedule remote search
  if (query.isNotEmpty) {
    _debounceTimer =
        Timer(_debounceDuration, () => _performRemoteSearch(query));
  }
}