introduce error boundary in call member

This commit is contained in:
Bruno Windels 2023-01-09 13:50:27 +01:00
parent f0d2c19184
commit b1687d7115

View File

@ -19,6 +19,7 @@ import {makeTxnId, makeId} from "../../common";
import {EventType, CallErrorCode} from "../callEventTypes";
import {formatToDeviceMessagesPayload} from "../../common";
import {sortedIndex} from "../../../utils/sortedIndex";
import { ErrorBoundary } from "../../../utils/ErrorBoundary";
import type {MuteSettings} from "../common";
import type {Options as PeerCallOptions, RemoteMedia} from "../PeerCall";
@ -94,6 +95,9 @@ class MemberConnection {
export class Member {
private connection?: MemberConnection;
private expireTimeout?: Timeout;
private errorBoundary = new ErrorBoundary(err => {
this.options.emitUpdate(this, "error");
});
constructor(
public member: RoomMember,
@ -104,6 +108,10 @@ export class Member {
this._renewExpireTimeout(updateMemberLog);
}
get error(): Error | undefined {
return this.errorBoundary.error;
}
private _renewExpireTimeout(log: ILogItem) {
this.expireTimeout?.dispose();
this.expireTimeout = undefined;
@ -166,6 +174,7 @@ export class Member {
/** @internal */
connect(localMedia: LocalMedia, localMuteSettings: MuteSettings, turnServer: BaseObservableValue<RTCIceServer>, memberLogItem: ILogItem): ILogItem | undefined {
return this.errorBoundary.try(() => {
if (this.connection) {
return;
}
@ -177,12 +186,14 @@ export class Member {
memberLogItem
);
this.connection = connection;
let connectLogItem;
let connectLogItem: ILogItem | undefined;
connection.logItem.wrap("connect", async log => {
connectLogItem = log;
await this.callIfNeeded(log);
});
throw new Error("connect failed!");
return connectLogItem;
});
}
private callIfNeeded(log: ILogItem): Promise<void> {
@ -211,6 +222,7 @@ export class Member {
/** @internal */
disconnect(hangup: boolean): ILogItem | undefined {
return this.errorBoundary.try(() => {
const {connection} = this;
if (!connection) {
return;
@ -226,15 +238,18 @@ export class Member {
connection.dispose();
this.connection = undefined;
return disconnectLogItem;
});
}
/** @internal */
updateCallInfo(callDeviceMembership: CallDeviceMembership, causeItem: ILogItem) {
this.errorBoundary.try(() => {
this.callDeviceMembership = callDeviceMembership;
this._renewExpireTimeout(causeItem);
if (this.connection) {
this.connection.logItem.refDetached(causeItem);
}
});
}
/** @internal */
@ -308,6 +323,7 @@ export class Member {
/** @internal */
handleDeviceMessage(message: SignallingMessage<MGroupCallBase>, syncLog: ILogItem): void {
this.errorBoundary.try(() => {
const {connection} = this;
if (connection) {
const destSessionId = message.content.dest_session_id;
@ -351,6 +367,7 @@ export class Member {
} else {
syncLog.log({l: "member not connected", userId: this.userId, deviceId: this.deviceId});
}
});
}
private dequeueSignallingMessages(connection: MemberConnection, peerCall: PeerCall, newMessage: SignallingMessage<MGroupCallBase>, syncLog: ILogItem): boolean {
@ -373,19 +390,23 @@ export class Member {
/** @internal */
async setMedia(localMedia: LocalMedia, previousMedia: LocalMedia): Promise<void> {
return this.errorBoundary.try(async () => {
const {connection} = this;
if (connection) {
connection.localMedia = localMedia.replaceClone(connection.localMedia, previousMedia);
await connection.peerCall?.setMedia(connection.localMedia, connection.logItem);
}
});
}
async setMuted(muteSettings: MuteSettings): Promise<void> {
return this.errorBoundary.try(async () => {
const {connection} = this;
if (connection) {
connection.localMuteSettings = muteSettings;
await connection.peerCall?.setMuted(muteSettings, connection.logItem);
}
});
}
private _createPeerCall(callId: string): PeerCall {