fix(members list): account for inconsistencies in users / members arrays

This commit is contained in:
Paul Makles
2022-01-29 11:09:24 +00:00
parent 7bed34810a
commit 5a81cd9743
2 changed files with 30 additions and 26 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "revolt.js",
"version": "5.2.7",
"version": "5.2.8",
"main": "dist/index.js",
"repository": "https://github.com/revoltchat/revolt.js",
"author": "Paul Makles <insrt.uk>",
+29 -25
View File
@@ -1,5 +1,6 @@
import type {
Category,
Member,
PermissionTuple,
Role,
Server as ServerI,
@@ -332,41 +333,44 @@ export class Server {
// ! FIXME: if we do this on the server, we can save 7ms locally
// 7ms to sort both lists.
data.users.sort((a,b)=>b._id.localeCompare(a._id));
data.members.sort((a,b)=>b._id.user.localeCompare(a._id.user));
// data.users.sort((a,b)=>b._id.localeCompare(a._id));
// data.members.sort((a,b)=>b._id.user.localeCompare(a._id.user));
// This takes roughly 23ms.
runInAction(() => {
// This adds roughly 15ms to 23ms above.
const mapping: Record<string, Member> = {};
data.members.forEach(x => mapping[x._id.user] = x);
for (let i=0;i<data.users.length;i++) {
if (data.users[i].online) {
const user = data.users[i];
this.client.users.createObj(user);
this.client.members.createObj(mapping[user._id]);
}
}
});
if (skipOffline) return;
let j = 0;
// Each batch takes between 70 and 90ms.
const batch = () => {
const offset = j * 100;
runInAction(() => {
for (let i=offset;i<data.users.length&&i<offset+100;i++) {
this.client.users.createObj(data.users[i]);
this.client.members.createObj(data.members[i]);
}
});
if (offset<data.users.length) {
j++;
setTimeout(batch, 0);
}
}
if (skipOffline) return;
let j = 0;
// Each batch takes between 70 and 90ms.
const batch = () => {
const offset = j * 100;
runInAction(() => {
for (let i=offset;i<data.users.length&&i<offset+100;i++) {
if (!data.users[i].online) {
this.client.users.createObj(data.users[i]);
this.client.members.createObj(data.members[i]);
}
}
});
if (offset<data.users.length) {
j++;
setTimeout(batch, 0);
}
}
setTimeout(batch, 0);
});
setTimeout(batch, 0);
}
/**