From ace59147abac65f1cd80279c33f8aa649994cfd2 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 21 Feb 2017 19:51:33 -0800 Subject: [PATCH] Reduce unnecessary updates on conversations at startup --- js/models/conversations.js | 7 +++++-- js/views/conversation_list_item_view.js | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 422bcb12..7a2ff6e2 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -188,12 +188,15 @@ return collection.fetchConversation(this.id, 1).then(function() { var lastMessage = collection.at(0); if (lastMessage) { - this.save({ + this.set({ lastMessage : lastMessage.getNotificationText(), timestamp : lastMessage.get('sent_at') }); } else { - this.save({ lastMessage: '', timestamp: null }); + this.set({ lastMessage: '', timestamp: null }); + } + if (this.hasChanged('lastMessage') || this.hasChanged('timestamp')) { + this.save(); } }.bind(this)); }, diff --git a/js/views/conversation_list_item_view.js b/js/views/conversation_list_item_view.js index ec34eda2..ec56ad90 100644 --- a/js/views/conversation_list_item_view.js +++ b/js/views/conversation_list_item_view.js @@ -20,10 +20,11 @@ this.listenTo(this.model, 'change', _.debounce(this.render.bind(this), 1000)); this.listenTo(this.model, 'destroy', this.remove); // auto update this.listenTo(this.model, 'opened', this.markSelected); // auto update - this.listenTo(this.model.messageCollection, 'add remove', - _.debounce(this.model.updateLastMessage.bind(this.model), 1000)); - this.listenTo(this.model, 'newmessage', - _.debounce(this.model.updateLastMessage.bind(this.model), 1000)); + + var updateLastMessage = _.debounce(this.model.updateLastMessage.bind(this.model), 1000); + this.listenTo(this.model.messageCollection, 'add remove', updateLastMessage); + this.listenTo(this.model, 'newmessage', updateLastMessage); + extension.windows.onClosed(this.stopListening.bind(this)); this.timeStampView = new Whisper.TimestampView({brief: true}); this.model.updateLastMessage();