mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 08:57:24 +08:00
PERF: Debounce mention lookup, enforce minimum username
This commit is contained in:
parent
0083697dff
commit
c2dde1ae88
@ -14,43 +14,30 @@ function updateFound($mentions, usernames) {
|
|||||||
const username = usernames[i];
|
const username = usernames[i];
|
||||||
if (found.indexOf(username) !== -1) {
|
if (found.indexOf(username) !== -1) {
|
||||||
replaceSpan($e, username);
|
replaceSpan($e, username);
|
||||||
} else {
|
} else if (checked.indexOf(username) !== -1) {
|
||||||
$e.removeClass('mention-loading').addClass('mention-tested');
|
$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;
|
return [];
|
||||||
export default function linkMentions($elem) {
|
}
|
||||||
if (linking) { return Ember.RSVP.Promise.resolve(); }
|
|
||||||
linking = true;
|
export function fetchUnseenMentions($elem, usernames) {
|
||||||
|
return Discourse.ajax("/users/is_local_username", { data: { usernames } }).then(function(r) {
|
||||||
return new Ember.RSVP.Promise(function(resolve) {
|
found.push.apply(found, r.valid);
|
||||||
const $mentions = $('span.mention:not(.mention-tested):not(.mention-loading)', $elem);
|
checked.push.apply(checked, usernames);
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ import afterTransition from 'discourse/lib/after-transition';
|
|||||||
import loadScript from 'discourse/lib/load-script';
|
import loadScript from 'discourse/lib/load-script';
|
||||||
import avatarTemplate from 'discourse/lib/avatar-template';
|
import avatarTemplate from 'discourse/lib/avatar-template';
|
||||||
import positioningWorkaround from 'discourse/lib/safari-hacks';
|
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, {
|
const ComposerView = Discourse.View.extend(Ember.Evented, {
|
||||||
_lastKeyTimeout: null,
|
_lastKeyTimeout: null,
|
||||||
@ -177,7 +177,17 @@ const ComposerView = Discourse.View.extend(Ember.Evented, {
|
|||||||
Discourse.Onebox.load(e, refresh);
|
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);
|
this.trigger('previewRefreshed', $wmdPreview);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user