From dccde8f3c7e8a200b683133e3fc02ad723d63f9d Mon Sep 17 00:00:00 2001 From: Daniel Salinas Date: Thu, 29 Feb 2024 16:17:10 -0500 Subject: [PATCH] Fix race condition with sliding sync extensions --- src/sliding-sync-sdk.ts | 6 +++--- src/sliding-sync.ts | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index bfe32448bd2..31f7c65f794 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -218,7 +218,7 @@ class ExtensionAccountData implements Extension { if (data.global && data.global.length > 0) { this.processGlobalAccountData(data.global); } @@ -288,7 +288,7 @@ class ExtensionTyping implements Extension { if (!data?.rooms) { return; } @@ -327,7 +327,7 @@ class ExtensionReceipts implements Extension { if (!data?.rooms) { return; } diff --git a/src/sliding-sync.ts b/src/sliding-sync.ts index a45a142d58e..6ab16b603d7 100644 --- a/src/sliding-sync.ts +++ b/src/sliding-sync.ts @@ -282,7 +282,7 @@ export interface Extension { * A function which is called when there is response JSON under this extension. * @param data - The response JSON under the extension name. */ - onResponse(data: Res): void; + onResponse(data: Res): Promise; /** * Controls when onResponse should be called. * @returns The state when it should be called. @@ -546,20 +546,20 @@ export class SlidingSync extends TypedEventEmitter): void { - Object.keys(ext).forEach((extName) => { + private async onPreExtensionsResponse(ext: Record): Promise { + await Promise.all(Object.keys(ext).map(async (extName) => { if (this.extensions[extName].when() == ExtensionState.PreProcess) { - this.extensions[extName].onResponse(ext[extName]); + await this.extensions[extName].onResponse(ext[extName]); } - }); + })); } - private onPostExtensionsResponse(ext: Record): void { - Object.keys(ext).forEach((extName) => { + private async onPostExtensionsResponse(ext: Record): Promise { + await Promise.all(Object.keys(ext).map(async (extName) => { if (this.extensions[extName].when() == ExtensionState.PostProcess) { - this.extensions[extName].onResponse(ext[extName]); + await this.extensions[extName].onResponse(ext[extName]); } - }); + })); } /** @@ -921,7 +921,7 @@ export class SlidingSync extends TypedEventEmitter { const list = this.lists.get(listKey); if (!list) {