Browse Source

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
JC Brand 3 years ago
parent
commit
d48d748e30
2 changed files with 6 additions and 9 deletions
  1. 5 8
      src/headless/plugins/chat/model.js
  2. 1 1
      src/headless/plugins/mam/utils.js

+ 5 - 8
src/headless/plugins/chat/model.js

@@ -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) {

+ 1 - 1
src/headless/plugins/mam/utils.js

@@ -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