PERF: Debounce mention lookup, enforce minimum username

This commit is contained in:
Robin Ward 2015-06-11 15:12:16 -04:00
parent c58b495e15
commit ef1d27fffc
2 changed files with 32 additions and 35 deletions

View File

@ -14,43 +14,30 @@ function updateFound($mentions, usernames) {
const username = usernames[i];
if (found.indexOf(username) !== -1) {
replaceSpan($e, username);
} else {
$e.removeClass('mention-loading').addClass('mention-tested');
} else if (checked.indexOf(username) !== -1) {
$e.addClass('mention-tested');
}
});
});
}
export function linkSeenMentions($elem, siteSettings) {
const $mentions = $('span.mention:not(.mention-tested)', $elem);
if ($mentions.length) {
const usernames = $mentions.map((_, e) => $(e).text().substr(1).toLowerCase());
const unseen = _.uniq(usernames).filter((u) => {
return u.length >= siteSettings.min_username_length && checked.indexOf(u) === -1;
});
updateFound($mentions, usernames);
return unseen;
}
let linking = false;
export default function linkMentions($elem) {
if (linking) { return Ember.RSVP.Promise.resolve(); }
linking = true;
return new Ember.RSVP.Promise(function(resolve) {
const $mentions = $('span.mention:not(.mention-tested):not(.mention-loading)', $elem);
if ($mentions.length) {
const usernames = $mentions.map((_, e) => $(e).text().substr(1).toLowerCase());
if (usernames.length) {
$mentions.addClass('mention-loading');
const uncached = _.uniq(usernames).filter((u) => { return checked.indexOf(u) === -1; });
if (uncached.length) {
return Discourse.ajax("/users/is_local_username", {
data: { usernames: uncached}
}).then(function(r) {
found.push.apply(found, r.valid);
checked.push.apply(checked, uncached);
updateFound($mentions, usernames);
resolve();
});
} else {
updateFound($mentions, usernames);
}
}
}
resolve();
}).finally(() => { linking = false });
return [];
}
export function fetchUnseenMentions($elem, usernames) {
return Discourse.ajax("/users/is_local_username", { data: { usernames } }).then(function(r) {
found.push.apply(found, r.valid);
checked.push.apply(checked, usernames);
});
}

View File

@ -3,7 +3,7 @@ import afterTransition from 'discourse/lib/after-transition';
import loadScript from 'discourse/lib/load-script';
import avatarTemplate from 'discourse/lib/avatar-template';
import positioningWorkaround from 'discourse/lib/safari-hacks';
import linkMentions from 'discourse/lib/link-mentions';
import { linkSeenMentions, fetchUnseenMentions } from 'discourse/lib/link-mentions';
const ComposerView = Discourse.View.extend(Ember.Evented, {
_lastKeyTimeout: null,
@ -177,7 +177,17 @@ const ComposerView = Discourse.View.extend(Ember.Evented, {
Discourse.Onebox.load(e, refresh);
});
linkMentions($wmdPreview).then(() => {
const unseen = linkSeenMentions($wmdPreview, this.siteSettings);
if (unseen.length) {
Ember.run.debounce(this, this._renderUnseen, $wmdPreview, unseen, 500);
}
this.trigger('previewRefreshed', $wmdPreview);
},
_renderUnseen: function($wmdPreview, unseen) {
fetchUnseenMentions($wmdPreview, unseen, this.siteSettings).then(() => {
linkSeenMentions($wmdPreview, this.siteSettings);
this.trigger('previewRefreshed', $wmdPreview);
});
},