diff --git a/spec/unit/matrixrtc/MatrixRTCSession.spec.ts b/spec/unit/matrixrtc/MatrixRTCSession.spec.ts index 51f9cef3f..8424d9704 100644 --- a/spec/unit/matrixrtc/MatrixRTCSession.spec.ts +++ b/spec/unit/matrixrtc/MatrixRTCSession.spec.ts @@ -419,9 +419,13 @@ describe("MatrixRTCSession", () => { sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom); }); - afterEach(() => { + afterEach(async () => { + const wasJoined = sess!.isJoined(); // stop the timers - sess!.leaveRoomSession(); + const left = await sess!.leaveRoomSession(); + if (left !== wasJoined) { + throw new Error(`Unexpected leave result: wanted ${wasJoined}, got ${left}`); + } }); it("starts un-joined", () => { diff --git a/src/matrixrtc/MatrixRTCSession.ts b/src/matrixrtc/MatrixRTCSession.ts index e8a2916d0..b35205519 100644 --- a/src/matrixrtc/MatrixRTCSession.ts +++ b/src/matrixrtc/MatrixRTCSession.ts @@ -38,6 +38,7 @@ import { MatrixError } from "../http-api/errors.ts"; import { MatrixEvent } from "../models/event.ts"; import { isLivekitFocusActive } from "./LivekitFocus.ts"; import { ExperimentalGroupCallRoomMemberState } from "../webrtc/groupCall.ts"; +import { sleep } from "../utils.ts"; const logger = rootLogger.getChild("MatrixRTCSession"); @@ -343,11 +344,12 @@ export class MatrixRTCSession extends TypedEventEmitter { if (!this.isJoined()) { logger.info(`Not joined to session in room ${this.room.roomId}: ignoring leave call`); - return new Promise((resolve) => resolve(false)); + return false; } const userId = this.client.getUserId(); @@ -377,19 +379,15 @@ export class MatrixRTCSession extends TypedEventEmitter { - if (timeout) { - // will never resolve if timeout is not set - setTimeout(r, timeout, "timeout"); - } - }); - return new Promise((resolve) => { - Promise.race([this.triggerCallMembershipEventUpdate(), timeoutPromise]).then((value) => { - // The timeoutPromise returns the string 'timeout' and the membership update void - // A success implies that the membership update was quicker then the timeout. - resolve(value != "timeout"); - }); - }); + if (timeout) { + // The sleep promise returns the string 'timeout' and the membership update void + // A success implies that the membership update was quicker then the timeout. + const raceResult = await Promise.race([this.triggerCallMembershipEventUpdate(), sleep(timeout, "timeout")]); + return raceResult !== "timeout"; + } else { + await this.triggerCallMembershipEventUpdate(); + return true; + } } public getActiveFocus(): Focus | undefined { @@ -1102,7 +1100,7 @@ export class MatrixRTCSession extends TypedEventEmitter setTimeout(resolve, resendDelay)); + await sleep(resendDelay); await this.triggerCallMembershipEventUpdate(); } }