ssb: Try harder to avoid a full re-render. It's disruptive.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled

This commit is contained in:
Cory McWilliams 2025-01-12 12:01:52 -05:00
parent 003e0caada
commit 0d597721bf
2 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🦀", "emoji": "🦀",
"previous": "&xWW9fuitFr6jFhe4fopPq2Zuu08ZBzHoKjFAAPx2a/U=.sha256" "previous": "&N69/NfCoe/HmdG5Hl/ulq/CDYLhDYzi50jbg2h3VPuc=.sha256"
} }

View File

@ -274,6 +274,11 @@ class TfTabNewsFeedElement extends LitElement {
return result; return result;
} }
merge_messages(old_messages, new_messages) {
let old_by_id = Object.fromEntries(old_messages.map(x => [x.id, x]));
return new_messages.map(x => old_by_id[x.id] ? old_by_id[x.id] : x);
}
async load_latest() { async load_latest() {
this.loading++; this.loading++;
let now = new Date().valueOf(); let now = new Date().valueOf();
@ -293,14 +298,14 @@ class TfTabNewsFeedElement extends LitElement {
} finally { } finally {
this.loading--; this.loading--;
} }
this.messages = Object.values( this.messages = this.merge_messages(this.messages, Object.values(
Object.fromEntries( Object.fromEntries(
[...this.messages, ...messages] [...this.messages, ...messages]
.sort((x, y) => x.timestamp - y.timestamp) .sort((x, y) => x.timestamp - y.timestamp)
.slice(-1024) .slice(-1024)
.map((x) => [x.id, x]) .map((x) => [x.id, x])
) )
); ));
console.log('done loading latest messages.'); console.log('done loading latest messages.');
} }
@ -309,8 +314,10 @@ class TfTabNewsFeedElement extends LitElement {
this.loading++; this.loading++;
let messages = []; let messages = [];
try { try {
this.messages = []; if (this._messages_hash !== this.hash) {
this._messages_hash = this.hash; this.messages = [];
this._messages_hash = this.hash;
}
this._messages_following = this.following; this._messages_following = this.following;
let now = new Date().valueOf(); let now = new Date().valueOf();
let start_time = now - 24 * 60 * 60 * 1000; let start_time = now - 24 * 60 * 60 * 1000;
@ -345,7 +352,7 @@ class TfTabNewsFeedElement extends LitElement {
} finally { } finally {
this.loading--; this.loading--;
} }
this.messages = messages; this.messages = this.merge_messages(this.messages, messages);
this.time_loading = undefined; this.time_loading = undefined;
console.log(`loading messages done for ${self.whoami}`); console.log(`loading messages done for ${self.whoami}`);
} }