Experimental: Lazy load user autocomplete entries

Loading the users into the autocomplete provider is quite a large
chunk of work for a large room. Try lazy loading it the first time
a completion is done rather than up front when the room is loaded,
given that a lot of the time you switch to a room you won't say
anything.
This commit is contained in:
David Baker 2017-09-22 14:31:29 +01:00
parent 45c4eeba01
commit 02894ee906

View file

@ -33,7 +33,8 @@ const USER_REGEX = /@\S*/g;
let instance = null; let instance = null;
export default class UserProvider extends AutocompleteProvider { export default class UserProvider extends AutocompleteProvider {
users: Array<RoomMember> = []; users: Array<RoomMember> = null;
room: Room = null;
constructor() { constructor() {
super(USER_REGEX, { super(USER_REGEX, {
@ -54,6 +55,9 @@ export default class UserProvider extends AutocompleteProvider {
return []; return [];
} }
// lazy-load user list into matcher
if (this.users === null) this._makeUsers();
let completions = []; let completions = [];
let {command, range} = this.getCurrentCommand(query, selection, force); let {command, range} = this.getCurrentCommand(query, selection, force);
if (command) { if (command) {
@ -83,7 +87,12 @@ export default class UserProvider extends AutocompleteProvider {
} }
setUserListFromRoom(room: Room) { setUserListFromRoom(room: Room) {
const events = room.getLiveTimeline().getEvents(); this.room = room;
this.users = null;
}
_makeUsers() {
const events = this.room.getLiveTimeline().getEvents();
const lastSpoken = {}; const lastSpoken = {};
for(const event of events) { for(const event of events) {
@ -91,7 +100,7 @@ export default class UserProvider extends AutocompleteProvider {
} }
const currentUserId = MatrixClientPeg.get().credentials.userId; const currentUserId = MatrixClientPeg.get().credentials.userId;
this.users = room.getJoinedMembers().filter((member) => { this.users = this.room.getJoinedMembers().filter((member) => {
if (member.userId !== currentUserId) return true; if (member.userId !== currentUserId) return true;
}); });
@ -103,6 +112,7 @@ export default class UserProvider extends AutocompleteProvider {
} }
onUserSpoke(user: RoomMember) { onUserSpoke(user: RoomMember) {
if (this.room === null) return;
if (user.userId === MatrixClientPeg.get().credentials.userId) return; if (user.userId === MatrixClientPeg.get().credentials.userId) return;
// Move the user that spoke to the front of the array // Move the user that spoke to the front of the array