Bullet-proof _setVerified and handleDataMessage against rejections

And the weird behavior we get from $.Deferred.

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-07-13 10:07:18 -07:00
parent 5da324103a
commit 4da1722ee8
2 changed files with 41 additions and 14 deletions

View file

@ -128,7 +128,9 @@
var keychange; var keychange;
return promise.then(function(updatedKey) { return promise.then(function(updatedKey) {
keychange = updatedKey; keychange = updatedKey;
return this.save({verified: verified}); return new Promise(function(resolve) {
return this.save({verified: verified}).always(resolve);
}.bind(this));
}.bind(this)).then(function() { }.bind(this)).then(function() {
// Three situations result in a verification notice in the conversation: // Three situations result in a verification notice in the conversation:
// 1) The message came from an explicit verification in another client (not // 1) The message came from an explicit verification in another client (not

View file

@ -351,10 +351,14 @@
if (dataMessage.group) { if (dataMessage.group) {
conversationId = dataMessage.group.id; conversationId = dataMessage.group.id;
} }
console.log('queuing handleDataMessage', source, timestamp);
var conversation = ConversationController.create({id: conversationId}); var conversation = ConversationController.create({id: conversationId});
conversation.queueJob(function() { conversation.queueJob(function() {
return new Promise(function(resolve) { return new Promise(function(resolve) {
conversation.fetch().always(function() { conversation.fetch().always(function() {
console.log('starting handleDataMessage', source, timestamp);
var now = new Date().getTime(); var now = new Date().getTime();
var attributes = { type: 'private' }; var attributes = { type: 'private' };
if (dataMessage.group) { if (dataMessage.group) {
@ -463,28 +467,49 @@
timestamp: message.get('sent_at') timestamp: message.get('sent_at')
}); });
} }
console.log('beginning saves in handleDataMessage', source, timestamp);
var handleError = function(error) {
error = error && error.stack ? error.stack : error;
console.log('handleDataMessage', source, timestamp, 'error:', error);
return resolve();
};
message.save().then(function() { message.save().then(function() {
conversation.save().then(function() { conversation.save().then(function() {
conversation.trigger('newmessage', message); try {
conversation.trigger('newmessage', message);
}
catch (e) {
return handleError(e);
}
// We fetch() here because, between the message.save() above and the previous // We fetch() here because, between the message.save() above and the previous
// line's trigger() call, we might have marked all messages unread in the // line's trigger() call, we might have marked all messages unread in the
// database. This message might already be read! // database. This message might already be read!
var previousUnread = message.get('unread'); var previousUnread = message.get('unread');
message.fetch().then(function() { message.fetch().then(function() {
if (previousUnread !== message.get('unread')) { try {
console.log('Caught race condition on new message read state! ' + if (previousUnread !== message.get('unread')) {
'Manually starting timers.'); console.log('Caught race condition on new message read state! ' +
// We call markRead() even though the message is already marked read 'Manually starting timers.');
// because we need to start expiration timers, etc. // We call markRead() even though the message is already marked read
message.markRead(); // because we need to start expiration timers, etc.
message.markRead();
}
if (message.get('unread')) {
conversation.notify(message);
}
console.log('done with handleDataMessage', source, timestamp);
return resolve();
} }
if (message.get('unread')) { catch (e) {
conversation.notify(message); handleError(e);
} }
resolve(); }, handleError);
}); }, handleError);
}); }, handleError);
});
}); });
}); });
}); });