Merge pull request #90 from whiteout-io/dev/WO-428

[WO-428] use integer to track busy state
This commit is contained in:
Tankred Hase 2014-07-17 17:53:07 +02:00
commit bfda677b1b
2 changed files with 61 additions and 28 deletions

View File

@ -47,7 +47,7 @@ define(function(require) {
keypair; keypair;
self._account = options.account; self._account = options.account;
self._account.busy = false; // triggers the spinner self._account.busy = 0; // triggers the spinner
self._account.online = false; self._account.online = false;
self._account.loggingIn = false; self._account.loggingIn = false;
@ -243,6 +243,7 @@ define(function(require) {
var self = this, var self = this,
folder = options.folder; folder = options.folder;
self.busy();
folder.messages = folder.messages || []; folder.messages = folder.messages || [];
self._localListMessages({ self._localListMessages({
folder: folder folder: folder
@ -283,7 +284,7 @@ define(function(require) {
}); });
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
updateUnreadCount(folder); // update the unread count updateUnreadCount(folder); // update the unread count
callback(err); callback(err);
} }
@ -302,7 +303,7 @@ define(function(require) {
var self = this, var self = this,
folder = options.folder; folder = options.folder;
self._account.busy = true; self.busy();
if (!self._account.online) { if (!self._account.online) {
done({ done({
@ -382,7 +383,7 @@ define(function(require) {
}); });
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
callback(err); callback(err);
} }
@ -450,7 +451,7 @@ define(function(require) {
folder = options.folder, folder = options.folder,
message = options.message; message = options.message;
self._account.busy = true; self.busy();
folder.messages.splice(folder.messages.indexOf(message), 1); folder.messages.splice(folder.messages.indexOf(message), 1);
@ -495,7 +496,7 @@ define(function(require) {
} }
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
if (err) { if (err) {
folder.messages.unshift(message); // re-add the message to the folder in case of an error folder.messages.unshift(message); // re-add the message to the folder in case of an error
} }
@ -518,11 +519,11 @@ define(function(require) {
folder = options.folder, folder = options.folder,
message = options.message; message = options.message;
self._account.busy = true; // start the spinner self.busy(); // start the spinner
// no-op if the message if not present anymore (for whatever reason) // no-op if the message if not present anymore (for whatever reason)
if (folder.messages.indexOf(message) < 0) { if (folder.messages.indexOf(message) < 0) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
return; return;
} }
@ -589,7 +590,7 @@ define(function(require) {
} }
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner // self.done(); // stop the spinner
updateUnreadCount(folder); // update the unread count updateUnreadCount(folder); // update the unread count
callback(err); callback(err);
} }
@ -613,6 +614,8 @@ define(function(require) {
message.loadingBody = true; message.loadingBody = true;
self.busy();
/* /*
* read this before inspecting the method! * read this before inspecting the method!
* *
@ -778,6 +781,7 @@ define(function(require) {
function done(err) { function done(err) {
self.done();
message.loadingBody = false; message.loadingBody = false;
callback(err, err ? undefined : message); callback(err, err ? undefined : message);
} }
@ -813,7 +817,10 @@ define(function(require) {
* @param {Function} callback(error, attachment) Invoked when the attachment body part was retrieved and parsed, or an error occurred * @param {Function} callback(error, attachment) Invoked when the attachment body part was retrieved and parsed, or an error occurred
*/ */
EmailDAO.prototype.getAttachment = function(options, callback) { EmailDAO.prototype.getAttachment = function(options, callback) {
this._getBodyParts({ var self = this;
self.busy();
self._getBodyParts({
folder: options.folder, folder: options.folder,
uid: options.uid, uid: options.uid,
bodyParts: [options.attachment] bodyParts: [options.attachment]
@ -822,7 +829,7 @@ define(function(require) {
callback(err); callback(err);
return; return;
} }
self.done();
// add the content to the original object // add the content to the original object
options.attachment.content = parsedBodyParts[0].content; options.attachment.content = parsedBodyParts[0].content;
callback(err, err ? undefined : options.attachment); callback(err, err ? undefined : options.attachment);
@ -848,11 +855,11 @@ define(function(require) {
message.decryptingBody = true; message.decryptingBody = true;
self.busy();
// get the sender's public key for signature checking // get the sender's public key for signature checking
self._keychain.getReceiverPublicKey(message.from[0].address, function(err, senderPublicKey) { self._keychain.getReceiverPublicKey(message.from[0].address, function(err, senderPublicKey) {
if (err) { if (err) {
done(err); return done(err);
return;
} }
// get the receiver's public key to check the message signature // get the receiver's public key to check the message signature
@ -873,8 +880,7 @@ define(function(require) {
if (encryptedNode._isPgpInline) { if (encryptedNode._isPgpInline) {
message.body = decrypted; message.body = decrypted;
message.decrypted = true; message.decrypted = true;
done(); return done();
return;
} }
// the mailparser works on the .raw property // the mailparser works on the .raw property
@ -885,8 +891,7 @@ define(function(require) {
bodyParts: [encryptedNode] bodyParts: [encryptedNode]
}, function(err, root) { }, function(err, root) {
if (err) { if (err) {
showError(err.errMsg || err.message); return showError(err.errMsg || err.message);
return;
} }
if (!message.signed) { if (!message.signed) {
@ -946,6 +951,7 @@ define(function(require) {
} }
function done(err) { function done(err) {
self.done();
message.decryptingBody = false; message.decryptingBody = false;
callback(err, err ? undefined : message); callback(err, err ? undefined : message);
} }
@ -968,6 +974,7 @@ define(function(require) {
return; return;
} }
self.busy();
// mime encode, sign, encrypt and send email via smtp // mime encode, sign, encrypt and send email via smtp
self._pgpMailer.send({ self._pgpMailer.send({
encrypt: true, encrypt: true,
@ -975,7 +982,10 @@ define(function(require) {
smtpclient: options.smtpclient, // filled solely in the integration test, undefined in normal usage smtpclient: options.smtpclient, // filled solely in the integration test, undefined in normal usage
mail: options.email, mail: options.email,
publicKeysArmored: options.email.publicKeysArmored publicKeysArmored: options.email.publicKeysArmored
}, callback); }, function(err) {
self.done();
callback(err);
});
}; };
/** /**
@ -985,19 +995,24 @@ define(function(require) {
* @param {Function} callback(error) Invoked when the message was sent, or an error occurred * @param {Function} callback(error) Invoked when the message was sent, or an error occurred
*/ */
EmailDAO.prototype.sendPlaintext = function(options, callback) { EmailDAO.prototype.sendPlaintext = function(options, callback) {
if (!this._account.online) { var self = this;
if (!self._account.online) {
callback({ callback({
errMsg: 'Client is currently offline!', errMsg: 'Client is currently offline!',
code: 42 code: 42
}); });
return; return;
} }
self.busy();
// mime encode, sign and send email via smtp // mime encode, sign and send email via smtp
this._pgpMailer.send({ self._pgpMailer.send({
smtpclient: options.smtpclient, // filled solely in the integration test, undefined in normal usage smtpclient: options.smtpclient, // filled solely in the integration test, undefined in normal usage
mail: options.email mail: options.email
}, callback); }, function(err) {
self.done();
callback(err);
});
}; };
/** /**
@ -1007,7 +1022,14 @@ define(function(require) {
* @param {Function} callback(error, message) Invoked when the message was encrypted, or an error occurred * @param {Function} callback(error, message) Invoked when the message was encrypted, or an error occurred
*/ */
EmailDAO.prototype.encrypt = function(options, callback) { EmailDAO.prototype.encrypt = function(options, callback) {
this._pgpbuilder.encrypt(options, callback); var self = this;
self.busy();
self._pgpbuilder.encrypt(options, function(err) {
self.done();
callback(err);
});
}; };
@ -1208,7 +1230,7 @@ define(function(require) {
var self = this, var self = this,
folderDbType = 'folders'; folderDbType = 'folders';
self._account.busy = true; // start the spinner self.busy(); // start the spinner
// fetch list from local cache // fetch list from local cache
self._devicestorage.listItems(folderDbType, 0, null, function(err, stored) { self._devicestorage.listItems(folderDbType, 0, null, function(err, stored) {
@ -1221,7 +1243,7 @@ define(function(require) {
}); });
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
callback(err); callback(err);
} }
}; };
@ -1237,7 +1259,7 @@ define(function(require) {
var self = this, var self = this,
folderDbType = 'folders'; folderDbType = 'folders';
self._account.busy = true; // start the spinner self.busy(); // start the spinner
// fetch list from imap server // fetch list from imap server
self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) { self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
@ -1297,7 +1319,7 @@ define(function(require) {
}); });
function done(err) { function done(err) {
self._account.busy = false; // stop the spinner self.done(); // stop the spinner
callback(err); callback(err);
} }
}; };
@ -1335,6 +1357,17 @@ define(function(require) {
}); });
}; };
EmailDAO.prototype.busy = function() {
this._account.busy++;
};
EmailDAO.prototype.done = function() {
if (this._account.busy > 0) {
this._account.busy--;
}
};
// //
// //
@ -1517,6 +1550,7 @@ define(function(require) {
// //
// //
/** /**
* Updates a folder's unread count: * Updates a folder's unread count:
* - For the outbox, that's the total number of messages, * - For the outbox, that's the total number of messages,

View File

@ -436,7 +436,6 @@ define(function(require) {
expect(notified).to.be.true; expect(notified).to.be.true;
expect(localStoreStub.calledOnce).to.be.true; expect(localStoreStub.calledOnce).to.be.true;
expect(imapListStub.calledOnce).to.be.true; expect(imapListStub.calledOnce).to.be.true;
expect(account.busy).to.be.false;
done(); done();
}); });