From bab3d7122cbf00b65fe156e14c85e06c8e4022d4 Mon Sep 17 00:00:00 2001 From: Timo Date: Thu, 17 Jul 2025 15:59:57 +0200 Subject: [PATCH] move to create opts + test --- spec/unit/matrix-client.spec.ts | 26 ++++++++++++++++++++++++++ spec/unit/matrixrtc/mocks.ts | 2 ++ src/client.ts | 31 ++++++++++++++++--------------- src/sync.ts | 1 - 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index f5299a346..b1ea2a84f 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1082,6 +1082,32 @@ describe("MatrixClient", function () { await client._unstable_updateDelayedEvent(delayId, action); }); + + it("uses custom timeout for restart delayed event", async () => { + // make another client so we can pass creation opts + makeClient({ delayedEventRestartLocalTimeoutMS: 2300 }); + const delayId = "id"; + const action = UpdateDelayedEventAction.Restart; + httpLookups = [ + { + method: "POST", + prefix: unstableMSC4140Prefix, + path: `/delayed_events/${encodeURIComponent(delayId)}`, + data: { + action, + }, + }, + ]; + await client._unstable_updateDelayedEvent(delayId, action); + + expect(client.http.authedRequest).toHaveBeenLastCalledWith( + "POST", + "/delayed_events/id", + undefined, + { action: "restart" }, + { localTimeoutMs: 2300, prefix: "/_matrix/client/unstable/org.matrix.msc4140" }, + ); + }); }); describe("extended profiles", () => { diff --git a/spec/unit/matrixrtc/mocks.ts b/spec/unit/matrixrtc/mocks.ts index d61670d79..a2b3e694e 100644 --- a/spec/unit/matrixrtc/mocks.ts +++ b/spec/unit/matrixrtc/mocks.ts @@ -45,6 +45,7 @@ export const membershipTemplate: SessionMembershipData & { user_id: string } = { export type MockClient = Pick< MatrixClient, + | "http" | "getUserId" | "getDeviceId" | "sendEvent" @@ -65,6 +66,7 @@ export function makeMockClient(userId: string, deviceId: string): MockClient { cancelPendingEvent: jest.fn(), _unstable_updateDelayedEvent: jest.fn(), _unstable_sendDelayedStateEvent: jest.fn(), + http: { authedRequest: jest.fn() } as any, // Mocking http request for compatibility }; } diff --git a/src/client.ts b/src/client.ts index 2de56d408..89b3807c9 100644 --- a/src/client.ts +++ b/src/client.ts @@ -340,6 +340,19 @@ export interface ICreateClientOpts { */ localTimeoutMs?: number; + /** + * The maximum amount of time to wait before timing out the `POST /_matrix/client/v1/delayed_events/{delay_id}` with `action = "restart"` requests. + * If not specified, it uses `localTimeoutMs` if set, otherwise there is no timeout. + * + * This setting is used in the context of MatrixRTC. We need to restart the dealyed events to make sure + * the HomeServer is sending the delayed rtc leave event. In bad network environments we might end up + * waiting for too long for the event to arrive and we will not send another restart event until the local timeout is reached. + * + * In those scenarios chances for success are higher if we use a lower local timeout to increase the tries we do instead of waiting + * for responses on requests which are stuck. + */ + delayedEventRestartLocalTimeoutMS?: number; + /** * Set to false to send the access token to the server via a query parameter rather * than the Authorization HTTP header. @@ -493,19 +506,6 @@ export interface IStartClientOpts { */ pollTimeout?: number; - /** - * The maximum amount of time to wait before timing out the `POST /_matrix/client/v1/delayed_events/{delay_id}` with `action = "restart"` requests. - * If not specified, the default `localTimeoutMs` will be used. - * - * This setting is used in the context of MatrixRTC. We need to restart the dealyed events to make sure - * the HomeServer is sending the delayed rtc leave event. In bad network environments we might end up - * waiting for too long for the event to arrive and we will not send another restart event until the local timeout is reached. - * - * In those scenarios chances for success are higher if we use a lower local timeout to increase the tries we do instead of waiting - * for responses on requests which are stuck. - */ - delayedEventRestartLocalTimeoutMS?: number; - /** * The filter to apply to /sync calls. */ @@ -1285,6 +1285,7 @@ export class MatrixClient extends TypedEventEmitter[0], { fetchFn: opts.fetchFn, baseUrl: opts.baseUrl, @@ -3487,7 +3488,7 @@ export class MatrixClient extends TypedEventEmitter