You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-06 12:02:40 +03:00
Fix race condition with sliding sync extensions (#4089)
* Fix race condition with sliding sync extensions * Fix types on sliding-sync spec test * Prettier fixes
This commit is contained in:
@@ -107,8 +107,8 @@ describe("SlidingSync", () => {
|
||||
onRequest: (initial) => {
|
||||
return { initial: initial };
|
||||
},
|
||||
onResponse: (res) => {
|
||||
return {};
|
||||
onResponse: async (res) => {
|
||||
return;
|
||||
},
|
||||
when: () => ExtensionState.PreProcess,
|
||||
};
|
||||
@@ -1572,7 +1572,7 @@ describe("SlidingSync", () => {
|
||||
onPreExtensionRequest = () => {
|
||||
return extReq;
|
||||
};
|
||||
onPreExtensionResponse = (resp) => {
|
||||
onPreExtensionResponse = async (resp) => {
|
||||
extensionOnResponseCalled = true;
|
||||
callbackOrder.push("onPreExtensionResponse");
|
||||
expect(resp).toEqual(extResp);
|
||||
@@ -1613,7 +1613,7 @@ describe("SlidingSync", () => {
|
||||
return undefined;
|
||||
};
|
||||
let responseCalled = false;
|
||||
onPreExtensionResponse = (resp) => {
|
||||
onPreExtensionResponse = async (resp) => {
|
||||
responseCalled = true;
|
||||
};
|
||||
httpBackend!
|
||||
@@ -1649,7 +1649,7 @@ describe("SlidingSync", () => {
|
||||
};
|
||||
let responseCalled = false;
|
||||
const callbackOrder: string[] = [];
|
||||
onPostExtensionResponse = (resp) => {
|
||||
onPostExtensionResponse = async (resp) => {
|
||||
expect(resp).toEqual(extResp);
|
||||
responseCalled = true;
|
||||
callbackOrder.push("onPostExtensionResponse");
|
||||
|
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
@@ -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.
|
||||
@@ -546,20 +546,24 @@ 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]);
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -921,7 +925,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]);
|
||||
@@ -938,7 +942,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) {
|
||||
|
Reference in New Issue
Block a user