Skip to content

Commit 5024fdd

Browse files
chrisbobbegnprice
authored andcommitted
autocomplete: Add cache param to MentionAutocompleteQuery.testUser
1 parent 381c802 commit 5024fdd

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/model/autocomplete.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ class MentionAutocompleteQuery {
88

99
final List<String> _lowercaseWords;
1010

11-
bool testUser(User user) {
11+
bool testUser(User user, AutocompleteDataCache cache) {
1212
// TODO test email too, not just name
1313
// TODO test with diacritics stripped, where appropriate
1414

15-
// TODO cache, elsewhere
16-
final List<String> nameWords = user.fullName.toLowerCase().split(' ');
15+
final List<String> nameWords = cache.nameWordsForUser(user);
1716

1817
int nameWordsIndex = 0;
1918
int queryWordsIndex = 0;
@@ -40,3 +39,15 @@ class MentionAutocompleteQuery {
4039
@override
4140
int get hashCode => Object.hash('MentionAutocompleteQuery', raw);
4241
}
42+
43+
class AutocompleteDataCache {
44+
final Map<int, List<String>> _nameWordsByUser = {};
45+
46+
List<String> nameWordsForUser(User user) {
47+
return _nameWordsByUser[user.userId] ??= user.fullName.toLowerCase().split(' ');
48+
}
49+
50+
void invalidateUser(int userId) {
51+
_nameWordsByUser.remove(userId);
52+
}
53+
}

test/model/autocomplete_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import '../example_data.dart' as eg;
88
void main() {
99
test('MentionAutocompleteQuery.testUser', () {
1010
doCheck(String rawQuery, User user, bool expected) {
11-
final result = MentionAutocompleteQuery(rawQuery).testUser(user);
11+
final result = MentionAutocompleteQuery(rawQuery)
12+
.testUser(user, AutocompleteDataCache());
1213
expected ? check(result).isTrue() : check(result).isFalse();
1314
}
1415

0 commit comments

Comments
 (0)