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) => {
|
onRequest: (initial) => {
|
||||||
return { initial: initial };
|
return { initial: initial };
|
||||||
},
|
},
|
||||||
onResponse: (res) => {
|
onResponse: async (res) => {
|
||||||
return {};
|
return;
|
||||||
},
|
},
|
||||||
when: () => ExtensionState.PreProcess,
|
when: () => ExtensionState.PreProcess,
|
||||||
};
|
};
|
||||||
@@ -1572,7 +1572,7 @@ describe("SlidingSync", () => {
|
|||||||
onPreExtensionRequest = () => {
|
onPreExtensionRequest = () => {
|
||||||
return extReq;
|
return extReq;
|
||||||
};
|
};
|
||||||
onPreExtensionResponse = (resp) => {
|
onPreExtensionResponse = async (resp) => {
|
||||||
extensionOnResponseCalled = true;
|
extensionOnResponseCalled = true;
|
||||||
callbackOrder.push("onPreExtensionResponse");
|
callbackOrder.push("onPreExtensionResponse");
|
||||||
expect(resp).toEqual(extResp);
|
expect(resp).toEqual(extResp);
|
||||||
@@ -1613,7 +1613,7 @@ describe("SlidingSync", () => {
|
|||||||
return undefined;
|
return undefined;
|
||||||
};
|
};
|
||||||
let responseCalled = false;
|
let responseCalled = false;
|
||||||
onPreExtensionResponse = (resp) => {
|
onPreExtensionResponse = async (resp) => {
|
||||||
responseCalled = true;
|
responseCalled = true;
|
||||||
};
|
};
|
||||||
httpBackend!
|
httpBackend!
|
||||||
@@ -1649,7 +1649,7 @@ describe("SlidingSync", () => {
|
|||||||
};
|
};
|
||||||
let responseCalled = false;
|
let responseCalled = false;
|
||||||
const callbackOrder: string[] = [];
|
const callbackOrder: string[] = [];
|
||||||
onPostExtensionResponse = (resp) => {
|
onPostExtensionResponse = async (resp) => {
|
||||||
expect(resp).toEqual(extResp);
|
expect(resp).toEqual(extResp);
|
||||||
responseCalled = true;
|
responseCalled = true;
|
||||||
callbackOrder.push("onPostExtensionResponse");
|
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) {
|
if (data.global && data.global.length > 0) {
|
||||||
this.processGlobalAccountData(data.global);
|
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) {
|
if (!data?.rooms) {
|
||||||
return;
|
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.
|
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) {
|
if (!data?.rooms) {
|
||||||
return;
|
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.
|
* A function which is called when there is response JSON under this extension.
|
||||||
* @param data - The response JSON under the extension name.
|
* @param data - The response JSON under the extension name.
|
||||||
*/
|
*/
|
||||||
onResponse(data: Res): void;
|
onResponse(data: Res): Promise<void>;
|
||||||
/**
|
/**
|
||||||
* Controls when onResponse should be called.
|
* Controls when onResponse should be called.
|
||||||
* @returns The state when it should be called.
|
* @returns The state when it should be called.
|
||||||
@@ -546,20 +546,24 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
|
|||||||
return ext;
|
return ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private onPreExtensionsResponse(ext: Record<string, object>): void {
|
private async onPreExtensionsResponse(ext: Record<string, object>): Promise<void> {
|
||||||
Object.keys(ext).forEach((extName) => {
|
await Promise.all(
|
||||||
if (this.extensions[extName].when() == ExtensionState.PreProcess) {
|
Object.keys(ext).map(async (extName) => {
|
||||||
this.extensions[extName].onResponse(ext[extName]);
|
if (this.extensions[extName].when() == ExtensionState.PreProcess) {
|
||||||
}
|
await this.extensions[extName].onResponse(ext[extName]);
|
||||||
});
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onPostExtensionsResponse(ext: Record<string, object>): void {
|
private async onPostExtensionsResponse(ext: Record<string, object>): Promise<void> {
|
||||||
Object.keys(ext).forEach((extName) => {
|
await Promise.all(
|
||||||
if (this.extensions[extName].when() == ExtensionState.PostProcess) {
|
Object.keys(ext).map(async (extName) => {
|
||||||
this.extensions[extName].onResponse(ext[extName]);
|
if (this.extensions[extName].when() == ExtensionState.PostProcess) {
|
||||||
}
|
await this.extensions[extName].onResponse(ext[extName]);
|
||||||
});
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -921,7 +925,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
|
|||||||
if (!resp) {
|
if (!resp) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
this.onPreExtensionsResponse(resp.extensions);
|
await this.onPreExtensionsResponse(resp.extensions);
|
||||||
|
|
||||||
for (const roomId in resp.rooms) {
|
for (const roomId in resp.rooms) {
|
||||||
await this.invokeRoomDataListeners(roomId, resp!.rooms[roomId]);
|
await this.invokeRoomDataListeners(roomId, resp!.rooms[roomId]);
|
||||||
@@ -938,7 +942,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
|
this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
|
||||||
this.onPostExtensionsResponse(resp.extensions);
|
await this.onPostExtensionsResponse(resp.extensions);
|
||||||
listKeysWithUpdates.forEach((listKey: string) => {
|
listKeysWithUpdates.forEach((listKey: string) => {
|
||||||
const list = this.lists.get(listKey);
|
const list = this.lists.get(listKey);
|
||||||
if (!list) {
|
if (!list) {
|
||||||
|
Reference in New Issue
Block a user