Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Commit

Permalink
use simpler background fetcher and fetch incoming messages too
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophWurst committed Aug 25, 2016
1 parent 1f89ca8 commit b26e7ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 59 deletions.
3 changes: 0 additions & 3 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
55 changes: 5 additions & 50 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down
30 changes: 26 additions & 4 deletions js/controller/foldercontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -152,6 +151,29 @@ 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) {
console.log(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,
Expand Down
2 changes: 0 additions & 2 deletions js/views/navigation-accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit b26e7ff

Please sign in to comment.