@@ -250,25 +250,41 @@ abstract class AutocompleteView<QueryT extends AutocompleteQuery, ResultT extend
250
250
}
251
251
252
252
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 {
253
271
assert (_query != null );
254
272
final query = _query! ;
255
- final List <ResultT > results = [];
256
- final Iterable <CandidateT > data = getSortedItemsToTest ();
257
273
258
- final iterator = data .iterator;
274
+ final iterator = candidates .iterator;
259
275
outer: while (true ) {
260
276
assert (_query == query);
261
- if (await shouldStop ()) return null ;
277
+ if (await shouldStop ()) return true ;
262
278
assert (_query == query);
263
279
264
280
for (int i = 0 ; i < 1000 ; i++ ) {
265
281
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);
268
284
if (result != null ) results.add (result);
269
285
}
270
286
}
271
- return results ;
287
+ return false ;
272
288
}
273
289
}
274
290
0 commit comments