diff --git a/js/app.js b/js/app.js index ea13c8dc..1fdd24b6 100644 --- a/js/app.js +++ b/js/app.js @@ -93,9 +93,6 @@ define(function(require) { this.setUpSearch(); $.when(AccountController.loadAccounts()).done(function(accounts) { - // Start fetching messages in background - require('background').messageFetcher.start(); - this.router = new Router({ controller: new RouteController(accounts) }); diff --git a/js/background.js b/js/background.js index f0755e7c..71deac05 100644 --- a/js/background.js +++ b/js/background.js @@ -19,6 +19,7 @@ define(function(require) { var Cache = require('cache'); var Radio = require('radio'); var State = require('state'); + var MessageCollection = require('models/messagecollection'); /*jshint maxparams: 6 */ function showNotification(title, body, tag, icon, account, folder) { @@ -119,6 +120,7 @@ define(function(require) { // update folder status var changedAccount = accounts.get(changes.accountId); + var changedFolder = changedAccount.getFolderById(changes.id); var localFolder = folders.get(changes.id); localFolder.set('uidvalidity', changes.uidvalidity); localFolder.set('uidnext', changes.uidnext); @@ -128,8 +130,9 @@ define(function(require) { // reload if current selected folder has changed if (State.currentAccount === changedAccount && State.currentFolder.get('id') === changes.id) { - Radio.ui.request('messagesview:collection'). - add(changes.messages); + Radio.ui.request('messagesview:collection').add(changes.messages); + var messages = new MessageCollection(changes.messages).slice(0); + Radio.message.trigger('fetch:bodies', changedAccount, changedFolder, messages); } // Save new messages to the cached message list @@ -145,56 +148,8 @@ define(function(require) { }); }); } - /** - * Fetch message of the current account/folder in background - * - * Uses a queue where message IDs are stored and fetched periodically - * The message is only fetched if it's not already cached - */ - function MessageFetcher() { - var account = null; - var folder = null; - var pollIntervall = 3 * 1000; - var queue = []; - var timer = null; - - function fetch() { - if (queue.length > 0) { - // Empty waiting queue - var messages = queue; - queue = []; - - var fetchingMessageBodies = Radio.message.request('bodies', account, folder, messages); - $.when(fetchingMessageBodies).done(function(messages) { - require('cache').addMessages(account, folder, messages); - }); - } - } - - return { - start: function() { - account = State.currentAccount; - folder = State.currentFolder; - timer = setInterval(fetch, pollIntervall); - }, - restart: function() { - // Stop previous fetcher - clearInterval(timer); - - // Clear waiting queue - queue.length = 0; - - // Start again - this.start(); - }, - push: function(message) { - queue.push(message); - } - }; - } return { - messageFetcher: new MessageFetcher(), checkForNotifications: checkForNotifications, showNotification: showNotification, showMailNotification: showMailNotification diff --git a/js/controller/foldercontroller.js b/js/controller/foldercontroller.js index 3367cbf0..900d9c76 100644 --- a/js/controller/foldercontroller.js +++ b/js/controller/foldercontroller.js @@ -25,6 +25,8 @@ define(function(require) { var Radio = require('radio'); var FolderService = require('service/folderservice'); + Radio.message.on('fetch:bodies', fetchBodies); + /** * @param {Account} account * @returns {undefined} @@ -89,10 +91,7 @@ define(function(require) { if (messages.length > 0) { // Fetch first 10 messages in background - _.each(messages.slice(0, 10), function( - message) { - require('background').messageFetcher.push(message.get('id')); - }); + Radio.message.trigger('fetch:bodies', account, folder, messages.slice(0, 10)); Radio.message.trigger('load', account, folder, messages.first()); @@ -152,6 +151,28 @@ define(function(require) { loadFolderMessagesDebounced(account, folder, false, query); } + /** + * Fetch and cache messages in the background + * + * The message is only fetched if it has not been cached already + * + * @param {Account} account + * @param {Folder} folder + * @param {Message[]} messages + * @returns {undefined} + */ + function fetchBodies(account, folder, messages) { + if (messages.length > 0) { + var ids = _.map(messages, function(message) { + return message.get('id'); + }); + var fetchingMessageBodies = Radio.message.request('bodies', account, folder, ids); + $.when(fetchingMessageBodies).done(function(messages) { + require('cache').addMessages(account, folder, messages); + }); + } + } + return { loadAccountFolders: loadFolders, showFolder: showFolder, diff --git a/js/views/navigation-accounts.js b/js/views/navigation-accounts.js index 52265f12..c64027f2 100644 --- a/js/views/navigation-accounts.js +++ b/js/views/navigation-accounts.js @@ -55,8 +55,6 @@ define(function(require) { * @returns {undefined} */ onFolderChanged: function() { - // Stop background message fetcher of previous folder - require('background').messageFetcher.restart(); // hide message detail view on mobile // TODO: find better place for this $('#mail-message').addClass('hidden-mobile');