Skip to content

Commit d056570

Browse files
committed
move to create opts
1 parent 9308094 commit d056570

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

spec/unit/matrix-client.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,32 @@ describe("MatrixClient", function () {
10821082

10831083
await client._unstable_updateDelayedEvent(delayId, action);
10841084
});
1085+
1086+
it("uses custom timeout for restart delayed event", async () => {
1087+
// make another client so we can pass creation opts
1088+
makeClient({ delayedEventRestartLocalTimeoutMS: 2300 });
1089+
const delayId = "id";
1090+
const action = UpdateDelayedEventAction.Restart;
1091+
httpLookups = [
1092+
{
1093+
method: "POST",
1094+
prefix: unstableMSC4140Prefix,
1095+
path: `/delayed_events/${encodeURIComponent(delayId)}`,
1096+
data: {
1097+
action,
1098+
},
1099+
},
1100+
];
1101+
await client._unstable_updateDelayedEvent(delayId, action);
1102+
1103+
expect(client.http.authedRequest).toHaveBeenLastCalledWith(
1104+
"POST",
1105+
"/delayed_events/id",
1106+
undefined,
1107+
{ action: "restart" },
1108+
{ localTimeoutMs: 2300, prefix: "/_matrix/client/unstable/org.matrix.msc4140" },
1109+
);
1110+
});
10851111
});
10861112

10871113
describe("extended profiles", () => {

spec/unit/matrixrtc/mocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const membershipTemplate: SessionMembershipData = {
4444

4545
export type MockClient = Pick<
4646
MatrixClient,
47+
| "http"
4748
| "getUserId"
4849
| "getDeviceId"
4950
| "sendEvent"
@@ -64,6 +65,7 @@ export function makeMockClient(userId: string, deviceId: string): MockClient {
6465
cancelPendingEvent: jest.fn(),
6566
_unstable_updateDelayedEvent: jest.fn(),
6667
_unstable_sendDelayedStateEvent: jest.fn(),
68+
http: { authedRequest: jest.fn() } as any, // Mocking http request for compatibility
6769
};
6870
}
6971

src/client.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,19 @@ export interface ICreateClientOpts {
340340
*/
341341
localTimeoutMs?: number;
342342

343+
/**
344+
* The maximum amount of time to wait before timing out the `POST /_matrix/client/v1/delayed_events/{delay_id}` with `action = "restart"` requests.
345+
* If not specified, it uses `localTimeoutMs` if set, otherwise there is no timeout.
346+
*
347+
* This setting is used in the context of MatrixRTC. We need to restart the dealyed events to make sure
348+
* the HomeServer is sending the delayed rtc leave event. In bad network environments we might end up
349+
* waiting for too long for the event to arrive and we will not send another restart event until the local timeout is reached.
350+
*
351+
* In those scenarios chances for success are higher if we use a lower local timeout to increase the tries we do instead of waiting
352+
* for responses on requests which are stuck.
353+
*/
354+
delayedEventRestartLocalTimeoutMS?: number;
355+
343356
/**
344357
* Set to false to send the access token to the server via a query parameter rather
345358
* than the Authorization HTTP header.
@@ -493,19 +506,6 @@ export interface IStartClientOpts {
493506
*/
494507
pollTimeout?: number;
495508

496-
/**
497-
* The maximum amount of time to wait before timing out the `POST /_matrix/client/v1/delayed_events/{delay_id}` with `action = "restart"` requests.
498-
* If not specified, the default `localTimeoutMs` will be used.
499-
*
500-
* This setting is used in the context of MatrixRTC. We need to restart the dealyed events to make sure
501-
* the HomeServer is sending the delayed rtc leave event. In bad network environments we might end up
502-
* waiting for too long for the event to arrive and we will not send another restart event until the local timeout is reached.
503-
*
504-
* In those scenarios chances for success are higher if we use a lower local timeout to increase the tries we do instead of waiting
505-
* for responses on requests which are stuck.
506-
*/
507-
delayedEventRestartLocalTimeoutMS?: number;
508-
509509
/**
510510
* The filter to apply to /sync calls.
511511
*/
@@ -1282,6 +1282,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
12821282
protected txnCtr = 0;
12831283
protected mediaHandler = new MediaHandler(this);
12841284
protected sessionId: string;
1285+
protected delayedEventRestartLocalTimeoutMS: number | undefined;
12851286

12861287
/** IDs of events which are currently being encrypted.
12871288
*
@@ -1324,7 +1325,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
13241325

13251326
const userId = opts.userId || null;
13261327
this.credentials = { userId };
1327-
1328+
this.delayedEventRestartLocalTimeoutMS = opts.delayedEventRestartLocalTimeoutMS;
13281329
this.http = new MatrixHttpApi(this as ConstructorParameters<typeof MatrixHttpApi>[0], {
13291330
fetchFn: opts.fetchFn,
13301331
baseUrl: opts.baseUrl,
@@ -3492,7 +3493,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
34923493
action,
34933494
};
34943495
const opts = {
3495-
localTimeoutMs: action === "restart" ? this.clientOpts?.delayedEventRestartLocalTimeoutMS : undefined,
3496+
localTimeoutMs: action === "restart" ? this.delayedEventRestartLocalTimeoutMS : undefined,
34963497
...requestOptions,
34973498
};
34983499
return await this.http.authedRequest(Method.Post, path, undefined, data, {

src/sync.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ export function defaultClientOpts(opts?: IStoredClientOpts): IStoredClientOpts {
196196
initialSyncLimit: 8,
197197
resolveInvitesToProfiles: false,
198198
pollTimeout: 30 * 1000,
199-
delayedEventRestartLocalTimeoutMS: 2300,
200199
pendingEventOrdering: PendingEventOrdering.Chronological,
201200
threadSupport: false,
202201
...opts,

0 commit comments

Comments
 (0)