Fix race condition: Pull from database after add to conversation

Also add some console logs to help us determine whether this ever
happens to people in the wild.

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-05-26 15:33:50 -07:00
parent e9152d9d27
commit 10c2874d19
2 changed files with 20 additions and 4 deletions

View file

@ -303,6 +303,9 @@
var read = unreadMessages.map(function(m) {
if (this.messageCollection.get(m.id)) {
m = this.messageCollection.get(m.id);
} else {
console.log('Marked a message as read in the database, but ' +
'it was not in messageCollection.');
}
m.markRead();
return {

View file

@ -468,10 +468,23 @@
message.save().then(function() {
conversation.save().then(function() {
conversation.trigger('newmessage', message);
if (message.get('unread')) {
conversation.notify(message);
}
resolve();
// 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
// database. This message might already be read!
var previousUnread = message.get('unread');
message.fetch().then(function() {
if (previousUnread !== message.get('unread')) {
console.log('Caught race condition on new message read state! ' +
'Manually starting timers.');
// We call markRead() even though the message is already marked read
// because we need to start expiration timers, etc.
message.markRead();
}
if (message.get('unread')) {
conversation.notify(message);
}
resolve();
});
});
});
});