Skip to content

Commit

Permalink
Fix race condition with sliding sync extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
zzorba committed Feb 29, 2024
1 parent b2e0925 commit dccde8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class ExtensionAccountData implements Extension<ExtensionAccountDataRequest, Ext
};
}

public onResponse(data: ExtensionAccountDataResponse): void {
public async onResponse(data: ExtensionAccountDataResponse): Promise<void> {
if (data.global && data.global.length > 0) {
this.processGlobalAccountData(data.global);
}
Expand Down Expand Up @@ -288,7 +288,7 @@ class ExtensionTyping implements Extension<ExtensionTypingRequest, ExtensionTypi
};
}

public onResponse(data: ExtensionTypingResponse): void {
public async onResponse(data: ExtensionTypingResponse): Promise<void> {
if (!data?.rooms) {
return;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ class ExtensionReceipts implements Extension<ExtensionReceiptsRequest, Extension
return undefined; // don't send a JSON object for subsequent requests, we don't need to.
}

public onResponse(data: ExtensionReceiptsResponse): void {
public async onResponse(data: ExtensionReceiptsResponse): Promise<void> {
if (!data?.rooms) {
return;
}
Expand Down
22 changes: 11 additions & 11 deletions src/sliding-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export interface Extension<Req extends {}, Res extends {}> {
* 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<void>;
/**
* Controls when onResponse should be called.
* @returns The state when it should be called.
Expand Down Expand Up @@ -546,20 +546,20 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
return ext;
}

private onPreExtensionsResponse(ext: Record<string, object>): void {
Object.keys(ext).forEach((extName) => {
private async onPreExtensionsResponse(ext: Record<string, object>): Promise<void> {
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<string, object>): void {
Object.keys(ext).forEach((extName) => {
private async onPostExtensionsResponse(ext: Record<string, object>): Promise<void> {
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]);
}
});
}));
}

/**
Expand Down Expand Up @@ -921,7 +921,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
if (!resp) {
continue;
}
this.onPreExtensionsResponse(resp.extensions);
await this.onPreExtensionsResponse(resp.extensions);

for (const roomId in resp.rooms) {
await this.invokeRoomDataListeners(roomId, resp!.rooms[roomId]);
Expand All @@ -938,7 +938,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
}
}
this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
this.onPostExtensionsResponse(resp.extensions);
await this.onPostExtensionsResponse(resp.extensions);
listKeysWithUpdates.forEach((listKey: string) => {
const list = this.lists.get(listKey);
if (!list) {
Expand Down

0 comments on commit dccde8f

Please sign in to comment.