Skip to content

Commit 6617c10

Browse files
committed
autocomplete: Terminate the already-running search when there is a new one
Before this fix, when there was a search in progress in `_startSearch`, and then for some reason, `_startSearch` was called again, like through `refreshStaleUserResults`, the new search would do its work and update `_results` field, but also the current search would be retried and this in its turn, would also update `_results` in the same way as before, doing the same thing for the second time. This fix terminates the current in-progress search and leaves the room for the new search to do its work.
1 parent 42b1044 commit 6617c10

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/model/autocomplete.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,11 @@ class MentionAutocompleteView extends ChangeNotifier {
235235
_startSearch(MentionAutocompleteQuery query) async {
236236
List<MentionAutocompleteResult>? newResults;
237237

238-
while (true) {
239-
try {
240-
newResults = await _computeResults(query);
241-
break;
242-
} on ConcurrentModificationError {
243-
// Retry
244-
// TODO backoff?
245-
}
238+
try {
239+
newResults = await _computeResults(query);
240+
} on ConcurrentModificationError {
241+
// Terminate this search as there is a new search going on
242+
return;
246243
}
247244

248245
if (newResults == null) {

test/model/autocomplete_test.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,31 @@ void main() {
274274
}
275275
});
276276

277-
test('MentionAutocompleteView mutating store.users while in progress causes retry', () async {
277+
test('MentionAutocompleteView mutating store.users while in progress causes '
278+
'the results to be recomputed only one time ', () async {
278279
const narrow = CombinedFeedNarrow();
279280
final store = eg.store();
280281
for (int i = 0; i < 1500; i++) {
281282
await store.addUser(eg.user(userId: i, email: 'user$i@example.com', fullName: 'User $i'));
282283
}
283284
final view = MentionAutocompleteView.init(store: store, narrow: narrow);
284285

285-
bool done = false;
286-
view.addListener(() { done = true; });
286+
int notifiedCount = 0;
287+
view.addListener(() { notifiedCount++; });
287288
view.query = MentionAutocompleteQuery('User 10000');
288289

289290
await Future(() {});
290-
check(done).isFalse();
291+
check(notifiedCount).equals(0);
291292
await store.addUser(eg.user(userId: 10000, email: '[email protected]', fullName: 'User 10000'));
292293
await Future(() {});
293-
check(done).isFalse();
294+
check(notifiedCount).equals(0);
294295
await Future(() {});
295-
check(done).isTrue();
296+
check(notifiedCount).equals(1);
296297
check(view.results).single
297298
.isA<UserMentionAutocompleteResult>()
298299
.userId.equals(10000);
300+
await Future(() {});
301+
check(notifiedCount).equals(1);
299302
// new result sticks; no "zombie" result from `store.users` pre-mutation
300303
for (int i = 0; i < 10; i++) { // for good measure
301304
await Future(() {});

0 commit comments

Comments
 (0)