Skip to content

Commit bab3d71

Browse files
committed
move to create opts + test
1 parent bcd651d commit bab3d71

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
@@ -45,6 +45,7 @@ export const membershipTemplate: SessionMembershipData & { user_id: string } = {
4545

4646
export type MockClient = Pick<
4747
MatrixClient,
48+
| "http"
4849
| "getUserId"
4950
| "getDeviceId"
5051
| "sendEvent"
@@ -65,6 +66,7 @@ export function makeMockClient(userId: string, deviceId: string): MockClient {
6566
cancelPendingEvent: jest.fn(),
6667
_unstable_updateDelayedEvent: jest.fn(),
6768
_unstable_sendDelayedStateEvent: jest.fn(),
69+
http: { authedRequest: jest.fn() } as any, // Mocking http request for compatibility
6870
};
6971
}
7072

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
*/
@@ -1285,6 +1285,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
12851285
protected txnCtr = 0;
12861286
protected mediaHandler = new MediaHandler(this);
12871287
protected sessionId: string;
1288+
protected delayedEventRestartLocalTimeoutMS: number | undefined;
12881289

12891290
/** IDs of events which are currently being encrypted.
12901291
*
@@ -1327,7 +1328,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
13271328

13281329
const userId = opts.userId || null;
13291330
this.credentials = { userId };
1330-
1331+
this.delayedEventRestartLocalTimeoutMS = opts.delayedEventRestartLocalTimeoutMS;
13311332
this.http = new MatrixHttpApi(this as ConstructorParameters<typeof MatrixHttpApi>[0], {
13321333
fetchFn: opts.fetchFn,
13331334
baseUrl: opts.baseUrl,
@@ -3487,7 +3488,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
34873488
action,
34883489
};
34893490
const opts = {
3490-
localTimeoutMs: action === "restart" ? this.clientOpts?.delayedEventRestartLocalTimeoutMS : undefined,
3491+
localTimeoutMs: action === "restart" ? this.delayedEventRestartLocalTimeoutMS : undefined,
34913492
...requestOptions,
34923493
};
34933494
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
@@ -191,7 +191,6 @@ export function defaultClientOpts(opts?: IStoredClientOpts): IStoredClientOpts {
191191
initialSyncLimit: 8,
192192
resolveInvitesToProfiles: false,
193193
pollTimeout: 30 * 1000,
194-
delayedEventRestartLocalTimeoutMS: 2300,
195194
pendingEventOrdering: PendingEventOrdering.Chronological,
196195
threadSupport: false,
197196
...opts,

0 commit comments

Comments
 (0)