Skip to content

Commit 9e977fb

Browse files
committed
correct usage of methods that now became async
Signed-off-by: Timo K <[email protected]>
1 parent 606fb8d commit 9e977fb

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ describe("MatrixRTCSession", () => {
304304
let sentStateEvent: Promise<void>;
305305
beforeEach(async () => {
306306
sentStateEvent = new Promise((resolve) => {
307-
sendStateEventMock = jest.fn(resolve);
307+
sendStateEventMock = jest.fn().mockImplementation(() => {
308+
resolve();
309+
return Promise.resolve({ event_id: "id" });
310+
});
308311
});
309312
sendEventMock = jest.fn().mockResolvedValue(undefined);
310313
client.sendStateEvent = sendStateEventMock;
@@ -349,14 +352,8 @@ describe("MatrixRTCSession", () => {
349352

350353
sess!.joinRoomSession([mockFocus], mockFocus, { notificationType: "ring" });
351354
await Promise.race([sentStateEvent, new Promise((resolve) => setTimeout(resolve, 5000))]);
352-
const { resolve: r, promise: p } = Promise.withResolvers();
353-
sess?.once(MatrixRTCSessionEvent.JoinStateChanged, r);
354-
await p;
355355
mockRoomState(mockRoom, [{ ...membershipTemplate, user_id: client.getUserId()! }]);
356-
sess!.onRTCSessionMemberUpdate();
357-
const { resolve, promise } = Promise.withResolvers();
358-
sess?.once(MatrixRTCSessionEvent.MembershipsChanged, resolve);
359-
await promise;
356+
await sess!.onRTCSessionMemberUpdate();
360357
const ownMembershipId = sess?.memberships[0].eventId;
361358

362359
expect(client.sendEvent).toHaveBeenCalledWith(mockRoom!.roomId, EventType.RTCNotification, {
@@ -425,7 +422,8 @@ describe("MatrixRTCSession", () => {
425422
},
426423
]);
427424

428-
sess!.onRTCSessionMemberUpdate();
425+
await sess!.onRTCSessionMemberUpdate();
426+
429427
const ownMembershipId = sess?.memberships[0].eventId;
430428
expect(sess!.getConsensusCallIntent()).toEqual("audio");
431429

@@ -476,13 +474,13 @@ describe("MatrixRTCSession", () => {
476474
it("doesn't send a notification when joining an existing call", async () => {
477475
// Add another member to the call so that it is considered an existing call
478476
mockRoomState(mockRoom, [membershipTemplate]);
479-
sess!.onRTCSessionMemberUpdate();
477+
await sess!.onRTCSessionMemberUpdate();
480478

481479
// Simulate a join, including the update to the room state
482480
sess!.joinRoomSession([mockFocus], mockFocus, { notificationType: "ring" });
483481
await Promise.race([sentStateEvent, new Promise((resolve) => setTimeout(resolve, 5000))]);
484482
mockRoomState(mockRoom, [membershipTemplate, { ...membershipTemplate, user_id: client.getUserId()! }]);
485-
sess!.onRTCSessionMemberUpdate();
483+
await sess!.onRTCSessionMemberUpdate();
486484

487485
expect(client.sendEvent).not.toHaveBeenCalled();
488486
});
@@ -494,9 +492,9 @@ describe("MatrixRTCSession", () => {
494492
// But this time we want to simulate a race condition in which we receive a state event
495493
// from someone else, starting the call before our own state event has been sent
496494
mockRoomState(mockRoom, [membershipTemplate]);
497-
sess!.onRTCSessionMemberUpdate();
495+
await sess!.onRTCSessionMemberUpdate();
498496
mockRoomState(mockRoom, [membershipTemplate, { ...membershipTemplate, user_id: client.getUserId()! }]);
499-
sess!.onRTCSessionMemberUpdate();
497+
await sess!.onRTCSessionMemberUpdate();
500498

501499
// We assume that the responsibility to send a notification, if any, lies with the other
502500
// participant that won the race
@@ -511,7 +509,7 @@ describe("MatrixRTCSession", () => {
511509

512510
const onMembershipsChanged = jest.fn();
513511
sess.on(MatrixRTCSessionEvent.MembershipsChanged, onMembershipsChanged);
514-
sess.onRTCSessionMemberUpdate();
512+
await sess.onRTCSessionMemberUpdate();
515513

516514
expect(onMembershipsChanged).not.toHaveBeenCalled();
517515
});
@@ -524,8 +522,8 @@ describe("MatrixRTCSession", () => {
524522
sess.on(MatrixRTCSessionEvent.MembershipsChanged, onMembershipsChanged);
525523

526524
mockRoomState(mockRoom, []);
527-
sess.onRTCSessionMemberUpdate();
528525

526+
await sess.onRTCSessionMemberUpdate();
529527
expect(onMembershipsChanged).toHaveBeenCalled();
530528
});
531529

@@ -708,14 +706,14 @@ describe("MatrixRTCSession", () => {
708706

709707
// member2 leaves triggering key rotation
710708
mockRoomState(mockRoom, [membershipTemplate]);
711-
sess.onRTCSessionMemberUpdate();
709+
await sess.onRTCSessionMemberUpdate();
712710

713711
// member2 re-joins which should trigger an immediate re-send
714712
const keysSentPromise2 = new Promise<EncryptionKeysEventContent>((resolve) => {
715713
sendEventMock.mockImplementation((_roomId, _evType, payload) => resolve(payload));
716714
});
717715
mockRoomState(mockRoom, [membershipTemplate, member2]);
718-
sess.onRTCSessionMemberUpdate();
716+
await sess.onRTCSessionMemberUpdate();
719717
// but, that immediate resend is throttled so we need to wait a bit
720718
jest.advanceTimersByTime(1000);
721719
const { keys } = await keysSentPromise2;
@@ -766,7 +764,7 @@ describe("MatrixRTCSession", () => {
766764
});
767765

768766
mockRoomState(mockRoom, [membershipTemplate, member2]);
769-
sess.onRTCSessionMemberUpdate();
767+
await sess.onRTCSessionMemberUpdate();
770768

771769
await keysSentPromise2;
772770

@@ -813,7 +811,7 @@ describe("MatrixRTCSession", () => {
813811
sendEventMock.mockClear();
814812

815813
// these should be a no-op:
816-
sess.onRTCSessionMemberUpdate();
814+
await sess.onRTCSessionMemberUpdate();
817815
expect(sendEventMock).toHaveBeenCalledTimes(0);
818816
expect(sess!.statistics.counters.roomEventEncryptionKeysSent).toEqual(1);
819817
} finally {
@@ -858,7 +856,7 @@ describe("MatrixRTCSession", () => {
858856
sendEventMock.mockClear();
859857

860858
// this should be a no-op:
861-
sess.onRTCSessionMemberUpdate();
859+
await sess.onRTCSessionMemberUpdate();
862860
expect(sendEventMock).toHaveBeenCalledTimes(0);
863861

864862
// advance time to avoid key throttling
@@ -873,7 +871,7 @@ describe("MatrixRTCSession", () => {
873871
});
874872

875873
// this should re-send the key
876-
sess.onRTCSessionMemberUpdate();
874+
await sess.onRTCSessionMemberUpdate();
877875

878876
await keysSentPromise2;
879877

@@ -931,8 +929,10 @@ describe("MatrixRTCSession", () => {
931929
});
932930

933931
mockRoomState(mockRoom, [membershipTemplate]);
934-
sess.onRTCSessionMemberUpdate();
935-
932+
const { promise, resolve } = Promise.withResolvers();
933+
sess.once(MatrixRTCSessionEvent.MembershipsChanged, resolve);
934+
await sess.onRTCSessionMemberUpdate();
935+
await promise;
936936
jest.advanceTimersByTime(KEY_DELAY);
937937
expect(sendKeySpy).toHaveBeenCalledTimes(1);
938938
// check that we send the key with index 1 even though the send gets delayed when leaving.
@@ -998,7 +998,7 @@ describe("MatrixRTCSession", () => {
998998
mockRoomState(mockRoom, members.slice(0, membersToTest - i));
999999
}
10001000

1001-
sess!.onRTCSessionMemberUpdate();
1001+
await sess!.onRTCSessionMemberUpdate();
10021002

10031003
// advance time to avoid key throttling
10041004
jest.advanceTimersByTime(10000);
@@ -1037,7 +1037,7 @@ describe("MatrixRTCSession", () => {
10371037
});
10381038

10391039
mockRoomState(mockRoom, [membershipTemplate, member2]);
1040-
sess.onRTCSessionMemberUpdate();
1040+
await sess.onRTCSessionMemberUpdate();
10411041

10421042
await new Promise((resolve) => {
10431043
realSetTimeout(resolve);
@@ -1064,7 +1064,7 @@ describe("MatrixRTCSession", () => {
10641064
manageMediaKeys: true,
10651065
useExperimentalToDeviceTransport: true,
10661066
});
1067-
sess.onRTCSessionMemberUpdate();
1067+
await sess.onRTCSessionMemberUpdate();
10681068

10691069
await keySentPromise;
10701070

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
734734
}
735735

736736
if (soonestExpiry != undefined) {
737-
this.expiryTimeout = setTimeout(this.onRTCSessionMemberUpdate, soonestExpiry);
737+
this.expiryTimeout = setTimeout(() => void this.onRTCSessionMemberUpdate(), soonestExpiry);
738738
}
739739
}
740740

@@ -806,8 +806,8 @@ export class MatrixRTCSession extends TypedEventEmitter<
806806
/**
807807
* Call this when something changed that may impacts the current MatrixRTC members in this session.
808808
*/
809-
public onRTCSessionMemberUpdate = (): void => {
810-
void this.recalculateSessionMembers();
809+
public onRTCSessionMemberUpdate = (): Promise<void> => {
810+
return this.recalculateSessionMembers();
811811
};
812812

813813
/**
@@ -841,7 +841,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
841841
this.emit(MatrixRTCSessionEvent.MembershipsChanged, oldMemberships, this.memberships);
842842
});
843843

844-
void this.membershipManager?.onRTCSessionMemberUpdate(this.memberships);
844+
await this.membershipManager?.onRTCSessionMemberUpdate(this.memberships);
845845
// The `ownMembership` will be set when calling `onRTCSessionMemberUpdate`.
846846
const ownMembership = this.membershipManager?.ownMembership;
847847
if (this.pendingNotificationToSend && ownMembership && oldMemberships.length === 0) {

src/matrixrtc/MatrixRTCSessionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
135135
// wasActiveAndKnown = session.memberships.length > 0 and
136136
// nowActive = session.memberships.length
137137
// Alternatively we would need to setup some event emission when the RTC session ended.
138-
session.onRTCSessionMemberUpdate();
138+
await session.onRTCSessionMemberUpdate();
139139

140140
const nowActive = session.memberships.length > 0;
141141

src/matrixrtc/MembershipManager.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export class MembershipManager
398398
return this.joinConfig?.useRtcMemberFormat ?? false;
399399
}
400400
// LOOP HANDLER:
401-
private async membershipLoopHandler(type: MembershipActionType): Promise<ActionUpdate> {
401+
private membershipLoopHandler(type: MembershipActionType): Promise<ActionUpdate> {
402402
switch (type) {
403403
case MembershipActionType.SendDelayedEvent: {
404404
// Before we start we check if we come from a state where we have a delay id.
@@ -418,19 +418,19 @@ export class MembershipManager
418418
case MembershipActionType.RestartDelayedEvent: {
419419
if (!this.state.delayId) {
420420
// Delay id got reset. This action was used to check if the hs canceled the delayed event when the join state got sent.
421-
return createInsertActionUpdate(MembershipActionType.SendDelayedEvent);
421+
return Promise.resolve(createInsertActionUpdate(MembershipActionType.SendDelayedEvent));
422422
}
423423
return this.restartDelayedEvent(this.state.delayId);
424424
}
425425
case MembershipActionType.SendScheduledDelayedLeaveEvent: {
426426
// We are already good
427427
if (!this.state.hasMemberStateEvent) {
428-
return { replace: [] };
428+
return Promise.resolve({ replace: [] });
429429
}
430430
if (this.state.delayId) {
431431
return this.sendScheduledDelayedLeaveEventOrFallbackToSendLeaveEvent(this.state.delayId);
432432
} else {
433-
return createInsertActionUpdate(MembershipActionType.SendLeaveEvent);
433+
return Promise.resolve(createInsertActionUpdate(MembershipActionType.SendLeaveEvent));
434434
}
435435
}
436436
case MembershipActionType.SendJoinEvent: {
@@ -442,7 +442,7 @@ export class MembershipManager
442442
case MembershipActionType.SendLeaveEvent: {
443443
// We are good already
444444
if (!this.state.hasMemberStateEvent) {
445-
return { replace: [] };
445+
return Promise.resolve({ replace: [] });
446446
}
447447
// This is only a fallback in case we do not have working delayed events support.
448448
// first we should try to just send the scheduled leave event
@@ -452,12 +452,12 @@ export class MembershipManager
452452
}
453453

454454
// HANDLERS (used in the membershipLoopHandler)
455-
private async sendOrResendDelayedLeaveEvent(): Promise<ActionUpdate> {
455+
private sendOrResendDelayedLeaveEvent(): Promise<ActionUpdate> {
456456
// We can reach this at the start of a call (where we do not yet have a membership: state.hasMemberStateEvent=false)
457457
// or during a call if the state event canceled our delayed event or caused by an unexpected error that removed our delayed event.
458458
// (Another client could have canceled it, the homeserver might have removed/lost it due to a restart, ...)
459459
// In the `then` and `catch` block we treat both cases differently. "if (this.state.hasMemberStateEvent) {} else {}"
460-
return await this.client
460+
return this.client
461461
._unstable_sendDelayedStateEvent(
462462
this.room.roomId,
463463
{
@@ -514,9 +514,9 @@ export class MembershipManager
514514
});
515515
}
516516

517-
private async cancelKnownDelayIdBeforeSendDelayedEvent(delayId: string): Promise<ActionUpdate> {
517+
private cancelKnownDelayIdBeforeSendDelayedEvent(delayId: string): Promise<ActionUpdate> {
518518
// Remove all running updates and restarts
519-
return await this.client
519+
return this.client
520520
._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Cancel)
521521
.then(() => {
522522
this.state.delayId = undefined;
@@ -557,7 +557,7 @@ export class MembershipManager
557557
this.emit(MembershipManagerEvent.ProbablyLeft, this.state.probablyLeft);
558558
}
559559

560-
private async restartDelayedEvent(delayId: string): Promise<ActionUpdate> {
560+
private restartDelayedEvent(delayId: string): Promise<ActionUpdate> {
561561
// Compute the duration until we expect the server to send the delayed leave event.
562562
const durationUntilServerDelayedLeave = this.state.expectedServerDelayLeaveTs
563563
? this.state.expectedServerDelayLeaveTs - Date.now()
@@ -579,7 +579,7 @@ export class MembershipManager
579579

580580
// The obvious choice here would be to use the `IRequestOpts` to set the timeout. Since this call might be forwarded
581581
// to the widget driver this information would get lost. That is why we mimic the AbortError using the race.
582-
return await Promise.race([
582+
return Promise.race([
583583
this.client._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Restart),
584584
abortPromise,
585585
])
@@ -618,8 +618,8 @@ export class MembershipManager
618618
});
619619
}
620620

621-
private async sendScheduledDelayedLeaveEventOrFallbackToSendLeaveEvent(delayId: string): Promise<ActionUpdate> {
622-
return await this.client
621+
private sendScheduledDelayedLeaveEventOrFallbackToSendLeaveEvent(delayId: string): Promise<ActionUpdate> {
622+
return this.client
623623
._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Send)
624624
.then(() => {
625625
this.state.hasMemberStateEvent = false;
@@ -646,8 +646,8 @@ export class MembershipManager
646646
});
647647
}
648648

649-
private async sendJoinEvent(): Promise<ActionUpdate> {
650-
return await this.client
649+
private sendJoinEvent(): Promise<ActionUpdate> {
650+
return this.client
651651
.sendStateEvent(
652652
this.room.roomId,
653653
this.useRtcMemberFormat ? EventType.RTCMembership : EventType.GroupCallMemberPrefix,
@@ -691,9 +691,9 @@ export class MembershipManager
691691
});
692692
}
693693

694-
private async updateExpiryOnJoinedEvent(): Promise<ActionUpdate> {
694+
private updateExpiryOnJoinedEvent(): Promise<ActionUpdate> {
695695
const nextExpireUpdateIteration = this.state.expireUpdateIterations + 1;
696-
return await this.client
696+
return this.client
697697
.sendStateEvent(
698698
this.room.roomId,
699699
this.useRtcMemberFormat ? EventType.RTCMembership : EventType.GroupCallMemberPrefix,
@@ -720,8 +720,8 @@ export class MembershipManager
720720
throw e;
721721
});
722722
}
723-
private async sendFallbackLeaveEvent(): Promise<ActionUpdate> {
724-
return await this.client
723+
private sendFallbackLeaveEvent(): Promise<ActionUpdate> {
724+
return this.client
725725
.sendStateEvent(
726726
this.room.roomId,
727727
this.useRtcMemberFormat ? EventType.RTCMembership : EventType.GroupCallMemberPrefix,

0 commit comments

Comments
 (0)