Commit 0975af1e429bd7b240f035de3051d6ea2cf38c75
fix perf issues with auto-complete/suggest since using locale compare
fixes #770Matt McKegg committed on 4/14/2018, 11:43:11 AM
Parent: 680a431c743ee01f79ddd270be7444e06f4c8cef
Files changed
modules/channel/obs/suggest.js | changed |
modules/profile/async/suggest.js | changed |
plugs/intl/sync/i18n.js | changed |
modules/channel/obs/suggest.js | ||
---|---|---|
@@ -4,16 +4,18 @@ | ||
4 | 4 | exports.needs = nest({ |
5 | 5 | 'channel.obs.recent': 'first', |
6 | 6 | 'channel.obs.subscribed': 'first', |
7 | 7 | 'channel.obs.mostActive': 'first', |
8 | + 'intl.sync.startsWith': 'first', | |
8 | 9 | 'keys.sync.id': 'first' |
9 | 10 | }) |
10 | 11 | |
11 | 12 | exports.gives = nest('channel.async.suggest') |
12 | 13 | |
13 | 14 | exports.create = function (api) { |
14 | 15 | var suggestions = null |
15 | 16 | var subscribed = null |
17 | + var matches = api.intl.sync.startsWith | |
16 | 18 | |
17 | 19 | return nest('channel.async.suggest', function () { |
18 | 20 | loadSuggestions() |
19 | 21 | return function (word) { |
@@ -31,9 +33,9 @@ | ||
31 | 33 | if (!suggestions) { |
32 | 34 | var id = api.keys.sync.id() |
33 | 35 | subscribed = api.channel.obs.subscribed(id) |
34 | 36 | var mostActive = api.channel.obs.mostActive() |
35 | - var contacts = computed([subscribed, mostActive], function (a, b) { | |
37 | + var channels = computed([subscribed, mostActive], function (a, b) { | |
36 | 38 | var result = Array.from(a) |
37 | 39 | b.forEach((item, i) => { |
38 | 40 | if (!result.includes(item[0])) { |
39 | 41 | result.push(item) |
@@ -41,9 +43,9 @@ | ||
41 | 43 | }) |
42 | 44 | return result |
43 | 45 | }) |
44 | 46 | |
45 | - suggestions = map(contacts, suggestion, {idle: true}) | |
47 | + suggestions = map(channels, suggestion, {idle: true}) | |
46 | 48 | watch(suggestions) |
47 | 49 | } |
48 | 50 | } |
49 | 51 | |
@@ -72,8 +74,4 @@ | ||
72 | 74 | } else { |
73 | 75 | return fallback || '' |
74 | 76 | } |
75 | 77 | } |
76 | - | |
77 | -function matches (text, startsWith) { | |
78 | - return text.slice(0, startsWith.length).localeCompare(startsWith, 'default', {sensitivity: 'base'}) === 0 | |
79 | -} |
modules/profile/async/suggest.js | ||
---|---|---|
@@ -5,8 +5,9 @@ | ||
5 | 5 | 'profile.obs.recentlyUpdated': 'first', |
6 | 6 | 'contact.obs.following': 'first', |
7 | 7 | 'about.obs.name': 'first', |
8 | 8 | 'about.obs.imageUrl': 'first', |
9 | + 'intl.sync.startsWith': 'first', | |
9 | 10 | 'keys.sync.id': 'first' |
10 | 11 | }) |
11 | 12 | |
12 | 13 | exports.gives = nest('profile.async.suggest') |
@@ -14,8 +15,9 @@ | ||
14 | 15 | exports.create = function (api) { |
15 | 16 | var suggestions = null |
16 | 17 | var recentSuggestions = null |
17 | 18 | var following = null |
19 | + var matches = api.intl.sync.startsWith | |
18 | 20 | |
19 | 21 | return nest('profile.async.suggest', function () { |
20 | 22 | loadSuggestions() |
21 | 23 | return function (word, defaultItems) { |
@@ -97,8 +99,4 @@ | ||
97 | 99 | if (following.includes(id)) { |
98 | 100 | return 'following' |
99 | 101 | } |
100 | 102 | } |
101 | - | |
102 | -function matches (text, startsWith) { | |
103 | - return text.slice(0, startsWith.length).localeCompare(startsWith, 'default', {sensitivity: 'base'}) === 0 | |
104 | -} |
plugs/intl/sync/i18n.js | ||
---|---|---|
@@ -9,9 +9,10 @@ | ||
9 | 9 | 'locales', |
10 | 10 | 'localeNames', |
11 | 11 | 'i18n', |
12 | 12 | 'i18n_n', |
13 | - 'time' | |
13 | + 'time', | |
14 | + 'startsWith' | |
14 | 15 | ]) |
15 | 16 | |
16 | 17 | exports.needs = nest({ |
17 | 18 | 'intl.sync.locale': 'first', |
@@ -22,17 +23,25 @@ | ||
22 | 23 | |
23 | 24 | exports.create = (api) => { |
24 | 25 | let _locale |
25 | 26 | |
27 | + // TODO: this should probably follow the selected language | |
28 | + var collator = new Intl.Collator('default', {sensitivity: 'base', usage: 'search'}) | |
29 | + | |
26 | 30 | return nest('intl.sync', { |
27 | 31 | locale, |
28 | 32 | locales, |
33 | + startsWith, | |
29 | 34 | localeNames, |
30 | 35 | i18n, |
31 | 36 | i18n_n: i18nN, |
32 | 37 | time |
33 | 38 | }) |
34 | 39 | |
40 | + function startsWith (text, startsWith) { | |
41 | + return collator.compare(text.slice(0, startsWith.length), startsWith) === 0 | |
42 | + } | |
43 | + | |
35 | 44 | // Get locale value in setting |
36 | 45 | function locale () { |
37 | 46 | return api.settings.obs.get('patchwork.lang') |
38 | 47 | } |
Built with git-ssb-web