MessageReceiver: Don't wait for app logic to start next decrypt

This commit is contained in:
Scott Nonnenberg 2019-02-05 10:04:38 -08:00
parent 041fe4be05
commit b69eea543c

View file

@ -224,7 +224,11 @@ MessageReceiver.prototype.extend({
window.log.error('websocket error'); window.log.error('websocket error');
}, },
dispatchAndWait(event) { dispatchAndWait(event) {
return Promise.all(this.dispatchEvent(event)); const promise = this.appPromise || Promise.resolve();
const appJobPromise = Promise.all(this.dispatchEvent(event));
const job = () => appJobPromise;
this.appPromise = promise.then(job, job);
}, },
onclose(ev) { onclose(ev) {
window.log.info( window.log.info(
@ -354,23 +358,34 @@ MessageReceiver.prototype.extend({
const { incoming } = this; const { incoming } = this;
this.incoming = []; this.incoming = [];
const dispatchEmpty = () => { const emitEmpty = () => {
window.log.info("MessageReceiver: emitting 'empty' event"); window.log.info("MessageReceiver: emitting 'empty' event");
const ev = new Event('empty'); const ev = new Event('empty');
return this.dispatchAndWait(ev); this.dispatchAndWait(ev);
}; };
const queueDispatch = () => { const waitForApplication = async () => {
window.log.info(
"MessageReceiver: finished processing messages after 'empty', now waiting for application"
);
const promise = this.appPromise || Promise.resolve();
this.appPromise = Promise.resolve();
// We don't await here because we don't this to gate future message processing
promise.then(emitEmpty, emitEmpty);
};
const waitForEmptyQueue = () => {
// resetting count to zero so everything queued after this starts over again // resetting count to zero so everything queued after this starts over again
this.count = 0; this.count = 0;
this.addToQueue(dispatchEmpty); this.addToQueue(waitForApplication);
}; };
// We first wait for all recently-received messages (this.incoming) to be queued, // 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 // then we queue a task to wait for the application to finish its processing, then
// processing is complete by the time it runs. // finally we emit the 'empty' event to the queue.
Promise.all(incoming).then(queueDispatch, queueDispatch); Promise.all(incoming).then(waitForEmptyQueue, waitForEmptyQueue);
}, },
drain() { drain() {
const { incoming } = this; const { incoming } = this;