From b91107efbc048478d8f7238d4f32e15cd7a87a6c Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Mon, 28 Sep 2020 09:43:21 -0700 Subject: [PATCH] Conversation.addSingleMessage: Use queue to maintain incoming order --- ts/models/conversations.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index a5e076e28c..d0f62abca6 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -96,6 +96,8 @@ export class ConversationModel extends window.Backbone.Model< inProgressFetch?: Promise; + incomingMessageQueue?: typeof window.PQueueType; + jobQueue?: typeof window.PQueueType; messageCollection?: MessageModelCollectionType; @@ -672,10 +674,20 @@ export class ConversationModel extends window.Backbone.Model< // For incoming messages, they might arrive while we're in the middle of a bulk fetch // from the database. We'll wait until that is done to process this newly-arrived // message. - async addIncomingMessage(message: MessageModel): Promise { - await this.inProgressFetch; + addIncomingMessage(message: MessageModel): void { + if (!this.incomingMessageQueue) { + this.incomingMessageQueue = new window.PQueue({ + concurrency: 1, + timeout: 1000 * 60 * 2, + }); + } - this.addSingleMessage(message); + // We use a queue here to ensure messages are added to the UI in the order received + this.incomingMessageQueue.add(async () => { + await this.inProgressFetch; + + this.addSingleMessage(message); + }); } format(): ConversationType | null | undefined {