From 63135a2337047b2c9b96ca07abb76156b144424e Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 9 Dec 2015 16:48:29 -0800 Subject: [PATCH] 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 --- js/background.js | 6 ++---- js/models/messages.js | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/js/background.js b/js/background.js index 8d661fc4..c2e19d8a 100644 --- a/js/background.js +++ b/js/background.js @@ -238,8 +238,8 @@ 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; messages.where({type: 'outgoing'}).forEach(function(message) { var deliveries = message.get('delivered') || 0; @@ -264,8 +264,6 @@ receipts.add({ timestamp: timestamp, source: pushMessage.source }); return; }); - }).fail(function() { - console.log('got delivery receipt for unknown message', pushMessage.source, timestamp); }); } })(); diff --git a/js/models/messages.js b/js/models/messages.js index 044dd271..e0c4ecc1 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -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.notify(message); });