You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Refactor/simplify Promises in MatrixRTCSession (#4466)
* Refactor/simplify Promises in MatrixRTCSession * Update src/matrixrtc/MatrixRTCSession.ts Co-authored-by: Hugh Nimmo-Smith <hughns@users.noreply.github.com> * Fix+document+test leaveRoomSession's return value * Throw instead of using expect in teardown because lint rules forbid using expect outside of test functions --------- Co-authored-by: Hugh Nimmo-Smith <hughns@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
23c4c9fd8a
commit
b99ff83785
@@ -419,9 +419,13 @@ describe("MatrixRTCSession", () => {
|
|||||||
sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);
|
sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(async () => {
|
||||||
|
const wasJoined = sess!.isJoined();
|
||||||
// stop the timers
|
// 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", () => {
|
it("starts un-joined", () => {
|
||||||
|
@@ -38,6 +38,7 @@ import { MatrixError } from "../http-api/errors.ts";
|
|||||||
import { MatrixEvent } from "../models/event.ts";
|
import { MatrixEvent } from "../models/event.ts";
|
||||||
import { isLivekitFocusActive } from "./LivekitFocus.ts";
|
import { isLivekitFocusActive } from "./LivekitFocus.ts";
|
||||||
import { ExperimentalGroupCallRoomMemberState } from "../webrtc/groupCall.ts";
|
import { ExperimentalGroupCallRoomMemberState } from "../webrtc/groupCall.ts";
|
||||||
|
import { sleep } from "../utils.ts";
|
||||||
|
|
||||||
const logger = rootLogger.getChild("MatrixRTCSession");
|
const logger = rootLogger.getChild("MatrixRTCSession");
|
||||||
|
|
||||||
@@ -343,11 +344,12 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
|
|||||||
* The membership update required to leave the session will retry if it fails.
|
* The membership update required to leave the session will retry if it fails.
|
||||||
* Without network connection the promise will never resolve.
|
* Without network connection the promise will never resolve.
|
||||||
* A timeout can be provided so that there is a guarantee for the promise to resolve.
|
* A timeout can be provided so that there is a guarantee for the promise to resolve.
|
||||||
|
* @returns Whether the membership update was attempted and did not time out.
|
||||||
*/
|
*/
|
||||||
public async leaveRoomSession(timeout: number | undefined = undefined): Promise<boolean> {
|
public async leaveRoomSession(timeout: number | undefined = undefined): Promise<boolean> {
|
||||||
if (!this.isJoined()) {
|
if (!this.isJoined()) {
|
||||||
logger.info(`Not joined to session in room ${this.room.roomId}: ignoring leave call`);
|
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();
|
const userId = this.client.getUserId();
|
||||||
@@ -377,19 +379,15 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
|
|||||||
this.membershipId = undefined;
|
this.membershipId = undefined;
|
||||||
this.emit(MatrixRTCSessionEvent.JoinStateChanged, false);
|
this.emit(MatrixRTCSessionEvent.JoinStateChanged, false);
|
||||||
|
|
||||||
const timeoutPromise = new Promise((r) => {
|
if (timeout) {
|
||||||
if (timeout) {
|
// The sleep promise returns the string 'timeout' and the membership update void
|
||||||
// will never resolve if timeout is not set
|
// A success implies that the membership update was quicker then the timeout.
|
||||||
setTimeout(r, timeout, "timeout");
|
const raceResult = await Promise.race([this.triggerCallMembershipEventUpdate(), sleep(timeout, "timeout")]);
|
||||||
}
|
return raceResult !== "timeout";
|
||||||
});
|
} else {
|
||||||
return new Promise((resolve) => {
|
await this.triggerCallMembershipEventUpdate();
|
||||||
Promise.race([this.triggerCallMembershipEventUpdate(), timeoutPromise]).then((value) => {
|
return true;
|
||||||
// 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");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getActiveFocus(): Focus | undefined {
|
public getActiveFocus(): Focus | undefined {
|
||||||
@@ -1102,7 +1100,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
const resendDelay = CALL_MEMBER_EVENT_RETRY_DELAY_MIN + Math.random() * 2000;
|
const resendDelay = CALL_MEMBER_EVENT_RETRY_DELAY_MIN + Math.random() * 2000;
|
||||||
logger.warn(`Failed to send call member event (retrying in ${resendDelay}): ${e}`);
|
logger.warn(`Failed to send call member event (retrying in ${resendDelay}): ${e}`);
|
||||||
await new Promise((resolve) => setTimeout(resolve, resendDelay));
|
await sleep(resendDelay);
|
||||||
await this.triggerCallMembershipEventUpdate();
|
await this.triggerCallMembershipEventUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user