diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index d69bd66a9..87a9d226b 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -56,12 +56,12 @@ MessageReceiver.prototype.extend({ return; } // possible 403 or network issue. Make an request to confirm - this.server.getDevices(this.number) + return this.server.getDevices(this.number) .then(this.connect.bind(this)) // No HTTP error? Reconnect .catch(function(e) { var ev = new Event('error'); ev.error = e; - this.dispatchAndWait(ev); + return this.dispatchAndWait(ev); }.bind(this)); }, handleRequest: function(request) { @@ -134,14 +134,17 @@ MessageReceiver.prototype.extend({ return this.dispatchAndWait(ev); }.bind(this); - var scheduleDispatch = function() { + var queueDispatch = function() { // resetting count to zero so everything queued after this starts over again this.count = 0; this.addToQueue(dispatchEmpty); }.bind(this); - Promise.all(incoming).then(scheduleDispatch, scheduleDispatch); + // We first wait for all recently-received messages (this.incoming) to be queued, + // then we add a task to emit the 'empty' event to the queue, so all message + // processing is complete by the time it runs. + Promise.all(incoming).then(queueDispatch, queueDispatch); }, updateProgress: function(count) { // count by 10s @@ -529,7 +532,6 @@ MessageReceiver.prototype.extend({ }, handleContacts: function(envelope, contacts) { console.log('contact sync'); - var eventTarget = this; var attachmentPointer = contacts.blob; return this.handleAttachment(attachmentPointer).then(function() { var results = []; @@ -539,7 +541,7 @@ MessageReceiver.prototype.extend({ var ev = new Event('contact'); ev.confirm = this.removeFromCache.bind(this, envelope); ev.contactDetails = contactDetails; - results.push(eventTarget.dispatchAndWait(ev)); + results.push(this.dispatchAndWait(ev)); if (contactDetails.verified) { results.push(this.handleVerified( @@ -554,7 +556,7 @@ MessageReceiver.prototype.extend({ var ev = new Event('contactsync'); ev.confirm = this.removeFromCache.bind(this, envelope); - results.push(eventTarget.dispatchAndWait(ev)); + results.push(this.dispatchAndWait(ev)); return Promise.all(results); }.bind(this));