Don't cache the most recent message.

Since messages are ordered by time, the call to get it isn't very
expensive and by not caching we avoid getting a stale cache value
This commit is contained in:
JC Brand 2021-11-20 12:52:50 +01:00
parent 33f3cb76ad
commit d48d748e30
2 changed files with 6 additions and 9 deletions

View File

@ -130,8 +130,7 @@ const ChatBox = ModelWithContact.extend({
}
},
afterMessagesFetched (messages) {
this.most_recent_cached_message = messages ? this.getMostRecentMessage(messages) : null;
afterMessagesFetched () {
/**
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from
* `sessionStorage` but **NOT** from the server.
@ -147,7 +146,6 @@ const ChatBox = ModelWithContact.extend({
log.info(`Not re-fetching messages for ${this.get('jid')}`);
return;
}
this.most_recent_cached_message = null;
this.messages.fetched_flag = true;
const resolve = this.messages.fetched.resolve;
this.messages.fetch({
@ -441,10 +439,9 @@ const ChatBox = ModelWithContact.extend({
}
},
getMostRecentMessage (messages) {
messages = messages || this.messages;
for (let i=messages.length-1; i>=0; i--) {
const message = messages.at(i);
getMostRecentMessage () {
for (let i=this.messages.length-1; i>=0; i--) {
const message = this.messages.at(i);
if (message.get('type') === this.get('message_type')) {
return message;
}
@ -906,7 +903,7 @@ const ChatBox = ModelWithContact.extend({
* before the collection has been fetched.
* @async
* @private
* @method _converse.ChatBox#queueMessageCreation
* @method _converse.ChatBox#createMessage
* @param { Object } attrs
*/
async createMessage (attrs, options) {

View File

@ -202,7 +202,7 @@ export function fetchNewestMessages (model) {
if (model.disable_mam) {
return;
}
const most_recent_msg = model.most_recent_cached_message;
const most_recent_msg = model.getMostRecentMessage();
// if clear_messages_on_reconnection is true, than any recent messages
// must have been received *after* connection and we instead must query