Refactor inbox collection listeners

Create a new collection type for the inbox which listens to events on
the main conversation cache. Also don't reload conversation info from
the database as often or when unnecessary.

Fixes #345

// FREEBIE
pull/749/head
lilia 10 years ago
parent 0509bb0f5d
commit 756875f235

@ -10,22 +10,34 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
var conversations = new Whisper.ConversationCollection(); var conversations = new Whisper.ConversationCollection();
var inboxCollection = new (Backbone.Collection.extend({
initialize: function() {
this.on('change:active_at', this.sort);
this.on('change:unreadCount', this.updateUnreadCount);
window.inbox = new Backbone.Collection([], { this.listenTo(conversations, 'add change:active_at', this.addActive);
},
comparator: function(model) { comparator: function(model) {
return -model.get('active_at'); return -model.get('active_at');
},
addActive: function(model) {
if (model.get('active_at')) {
this.add(model);
}
},
updateUnreadCount: function(model, count) {
var prev = model.previous('unreadCount') || 0;
if (count < prev) { // decreased
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
setUnreadCount(newUnreadCount);
storage.put("unreadCount", newUnreadCount);
}
} }
}); }))();
inbox.on('change:active_at', inbox.sort); window.getInboxCollection = function() {
inbox.on('change:unreadCount', function(model, count) { return inboxCollection;
var prev = model.previous('unreadCount') || 0; };
if (count < prev) { // decreased
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
setUnreadCount(newUnreadCount);
storage.put("unreadCount", newUnreadCount);
}
});
window.ConversationController = { window.ConversationController = {
get: function(id) { get: function(id) {
@ -33,9 +45,6 @@
}, },
create: function(attrs) { create: function(attrs) {
var conversation = conversations.add(attrs); var conversation = conversations.add(attrs);
if (conversation.get('active_at')) {
inbox.add(conversation);
}
return conversation; return conversation;
}, },
findOrCreatePrivateById: function(id) { findOrCreatePrivateById: function(id) {
@ -55,11 +64,7 @@
}); });
}, },
updateInbox: function() { updateInbox: function() {
conversations.fetchActive().then(function() { conversations.fetchActive();
inbox.reset(conversations.filter(function(model) {
return model.get('active_at');
}));
});
} }
}; };
@ -76,12 +81,16 @@
window.notifyConversation = function(message) { window.notifyConversation = function(message) {
var conversationId = message.get('conversationId'); var conversationId = message.get('conversationId');
var conversation = ConversationController.create({id: conversationId}); var conversation = ConversationController.get(conversationId);
if (!conversation) {
conversation = conversations.create({id: conversationId});
conversation.fetch();
}
if (inboxOpened) { if (inboxOpened) {
conversation.reload(); conversation.fetchMessages();
extension.windows.drawAttention(inboxWindowId); extension.windows.drawAttention(inboxWindowId);
} else if (Whisper.Notifications.isEnabled()) { } else if (Whisper.Notifications.isEnabled()) {
var sender = conversations.add({id: message.get('source')}); var sender = ConversationController.create({id: message.get('source')});
conversation.fetch().then(function() { conversation.fetch().then(function() {
sender.fetch().then(function() { sender.fetch().then(function() {
var notification = new Notification(sender.getTitle(), { var notification = new Notification(sender.getTitle(), {
@ -94,9 +103,7 @@
}; };
}); });
}); });
conversation.fetchMessages();
} else { } else {
conversation.reload();
openConversation(conversation); openConversation(conversation);
ConversationController.updateInbox(); ConversationController.updateInbox();
} }

@ -54,6 +54,7 @@
appWindow: this.model.appWindow appWindow: this.model.appWindow
}); });
$el = view.$el; $el = view.$el;
conversation.fetchContacts();
if (conversation.messageCollection.length === 0) { if (conversation.messageCollection.length === 0) {
$el.find('.message-list').addClass('loading'); $el.find('.message-list').addClass('loading');
} }
@ -61,6 +62,9 @@
$el.prependTo(this.el); $el.prependTo(this.el);
$el.find('.message-list').trigger('reset-scroll'); $el.find('.message-list').trigger('reset-scroll');
$el.trigger('force-resize'); $el.trigger('force-resize');
conversation.fetchMessages().then(function() {
$el.find('.message-list').removeClass('loading');
});
conversation.markRead(); conversation.markRead();
} }
}); });
@ -81,17 +85,18 @@
this.listenTo(this.newConversationView, 'open', this.listenTo(this.newConversationView, 'open',
this.openConversation.bind(this, null)); this.openConversation.bind(this, null));
this.inbox = new Whisper.ConversationListView({ var inboxCollection = bg.getInboxCollection();
this.inboxView = new Whisper.ConversationListView({
el : this.$('.conversations'), el : this.$('.conversations'),
collection : bg.inbox collection : inboxCollection
}).render(); }).render();
this.inbox.listenTo(bg.inbox, 'sort', this.inbox.render); this.inboxView.listenTo(inboxCollection, 'sort', this.inboxView.render);
new SocketView().render().$el.appendTo(this.$('.socket-status')); new SocketView().render().$el.appendTo(this.$('.socket-status'));
extension.windows.beforeUnload(function() { extension.windows.beforeUnload(function() {
this.inbox.stopListening(); this.inboxView.stopListening();
}.bind(this)); }.bind(this));
new Whisper.WindowControlsView({ new Whisper.WindowControlsView({
@ -104,7 +109,6 @@
}, },
openConversation: function(e, data) { openConversation: function(e, data) {
var conversation = data.conversation; var conversation = data.conversation;
conversation.reload();
this.conversation_stack.open(conversation); this.conversation_stack.open(conversation);
this.hideCompose(); this.hideCompose();
}, },

Loading…
Cancel
Save