Fix race between sync messages and receipts

Previously, when processing a backlog of sync messages and their
delivery receipts, we would fail to mark some messages as delivered even
though we got a receipt. This was due to an async race condition between
saving a sync message and fetching it after the receipt arrives.

Fix by re-ordering idb requests such that we save the message first and
fetch it after.

Fixes #479

// FREEBIE
This commit is contained in:
lilia 2015-12-09 16:48:29 -08:00
parent 077cba284f
commit 63135a2337
2 changed files with 4 additions and 6 deletions

View file

@ -238,8 +238,8 @@
return; return;
} }
messages.fetchSentAt(timestamp).then(function() { groups.fetchGroups(pushMessage.source).then(function() {
groups.fetchGroups(pushMessage.source).then(function() { messages.fetchSentAt(timestamp).then(function() {
var found = false; var found = false;
messages.where({type: 'outgoing'}).forEach(function(message) { messages.where({type: 'outgoing'}).forEach(function(message) {
var deliveries = message.get('delivered') || 0; var deliveries = message.get('delivered') || 0;
@ -264,8 +264,6 @@
receipts.add({ timestamp: timestamp, source: pushMessage.source }); receipts.add({ timestamp: timestamp, source: pushMessage.source });
return; return;
}); });
}).fail(function() {
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp);
}); });
} }
})(); })();

View file

@ -352,8 +352,8 @@
}); });
} }
conversation.save().then(function() { message.save().then(function() {
message.save().then(function() { conversation.save().then(function() {
conversation.trigger('newmessage', message); conversation.trigger('newmessage', message);
conversation.notify(message); conversation.notify(message);
}); });