Skip to content

Commit 737ccb8

Browse files
gnpricechrisbobbe
authored andcommitted
autocomplete [nfc]: Split search loop out as _filterCandidates
1 parent 145c2fd commit 737ccb8

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

lib/model/autocomplete.dart

+23-7
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,41 @@ abstract class AutocompleteView<QueryT extends AutocompleteQuery, ResultT extend
250250
}
251251

252252
Future<List<ResultT>?> _computeResults() async {
253+
final results = <ResultT>[];
254+
if (await filterCandidates(filter: testItem,
255+
candidates: getSortedItemsToTest(), results: results)) {
256+
return null;
257+
}
258+
return results;
259+
}
260+
261+
/// Examine the given candidates against `query`, adding matches to `results`.
262+
///
263+
/// This function chunks its work for interruption using [shouldStop],
264+
/// and returns true if the search was aborted.
265+
@protected
266+
Future<bool> filterCandidates<T>({
267+
required ResultT? Function(QueryT query, T candidate) filter,
268+
required Iterable<T> candidates,
269+
required List<ResultT> results,
270+
}) async {
253271
assert(_query != null);
254272
final query = _query!;
255-
final List<ResultT> results = [];
256-
final Iterable<CandidateT> data = getSortedItemsToTest();
257273

258-
final iterator = data.iterator;
274+
final iterator = candidates.iterator;
259275
outer: while (true) {
260276
assert(_query == query);
261-
if (await shouldStop()) return null;
277+
if (await shouldStop()) return true;
262278
assert(_query == query);
263279

264280
for (int i = 0; i < 1000; i++) {
265281
if (!iterator.moveNext()) break outer;
266-
final CandidateT item = iterator.current;
267-
final result = testItem(query, item);
282+
final item = iterator.current;
283+
final result = filter(query, item);
268284
if (result != null) results.add(result);
269285
}
270286
}
271-
return results;
287+
return false;
272288
}
273289
}
274290

0 commit comments

Comments
 (0)