From 2aac3b5cb978aafe1aa49c206d636ff21155437e Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Tue, 29 Mar 2022 15:30:55 -0700 Subject: [PATCH] Split `addSingleMessage` for better batching --- ts/models/conversations.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 4461c776ba22..774b16ddc928 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -1389,6 +1389,11 @@ export class ConversationModel extends window.Backbone message: MessageModel, { isJustSent }: { isJustSent: boolean } = { isJustSent: false } ): Promise { + await this.beforeAddSingleMessage(); + this.doAddSingleMessage(message, { isJustSent }); + } + + private async beforeAddSingleMessage(): Promise { if (!this.newMessageQueue) { this.newMessageQueue = new window.PQueue({ concurrency: 1, @@ -1400,7 +1405,12 @@ export class ConversationModel extends window.Backbone await this.newMessageQueue.add(async () => { await this.inProgressFetch; }); + } + private doAddSingleMessage( + message: MessageModel, + { isJustSent }: { isJustSent: boolean } + ): void { const { messagesAdded } = window.reduxActions.conversations; const { conversations } = window.reduxStore.getState(); const { messagesByConversation } = conversations; @@ -1414,7 +1424,7 @@ export class ConversationModel extends window.Backbone newestId && messageIds && messageIds[messageIds.length - 1] === newestId; if (isJustSent && existingConversation && !isLatestInMemory) { - await this.loadNewestMessages(undefined, undefined); + this.loadNewestMessages(undefined, undefined); } else { messagesAdded({ conversationId, @@ -4042,6 +4052,9 @@ export class ConversationModel extends window.Backbone const renderStart = Date.now(); + // Perform asynchronous tasks before entering the batching mode + await this.beforeAddSingleMessage(); + this.isInReduxBatch = true; batchDispatch(() => { try { @@ -4051,7 +4064,7 @@ export class ConversationModel extends window.Backbone const enableProfileSharing = Boolean( mandatoryProfileSharingEnabled && !this.get('profileSharing') ); - this.addSingleMessage(model, { isJustSent: true }); + this.doAddSingleMessage(model, { isJustSent: true }); const draftProperties = dontClearDraft ? {}