From db859d43e94358263508af66a33f4b1a417066fb Mon Sep 17 00:00:00 2001 From: Timo <16718859+toger5@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:07:08 +0100 Subject: [PATCH] Refactor livekit disconnect to use an effect hook. (#1925) --------- Signed-off-by: Timo K --- src/room/GroupCallView.tsx | 24 +++++++------------- src/room/InCallView.tsx | 45 +++++++++++++++++++++----------------- src/rtcSessionHelpers.ts | 3 --- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/room/GroupCallView.tsx b/src/room/GroupCallView.tsx index 24c1568c..2073dd20 100644 --- a/src/room/GroupCallView.tsx +++ b/src/room/GroupCallView.tsx @@ -44,10 +44,9 @@ import { useRoomAvatar } from "./useRoomAvatar"; import { useRoomName } from "./useRoomName"; import { useJoinRule } from "./useJoinRule"; import { InviteModal } from "./InviteModal"; -import { E2EEConfig, useLiveKit } from "../livekit/useLiveKit"; +import { E2EEConfig } from "../livekit/useLiveKit"; import { useUrlParams } from "../UrlParams"; import { E2eeType } from "../e2ee/e2eeType"; -import { useOpenIDSFU } from "../livekit/openIDSFU"; declare global { interface Window { @@ -136,13 +135,6 @@ export const GroupCallView: FC = ({ return { mode: E2eeType.NONE }; } }, [perParticipantE2EE, e2eeSharedKey]); - const sfuConfig = useOpenIDSFU(client, rtcSession); - const { livekitRoom, connState } = useLiveKit( - rtcSession, - muteStates, - sfuConfig, - e2eeConfig, - ); useEffect(() => { // this effect is only if we don't want to show the lobby (skipLobby = true) @@ -238,7 +230,8 @@ export const GroupCallView: FC = ({ sendInstantly, ); - await leaveRTCSession(rtcSession, livekitRoom); + // Only sends matrix leave event. The Livekit session will disconnect once the ActiveCall-view unmounts. + await leaveRTCSession(rtcSession); if ( !isPasswordlessUser && @@ -248,7 +241,7 @@ export const GroupCallView: FC = ({ history.push("/"); } }, - [rtcSession, livekitRoom, isPasswordlessUser, confineToRoom, history], + [rtcSession, isPasswordlessUser, confineToRoom, history], ); useEffect(() => { @@ -257,14 +250,15 @@ export const GroupCallView: FC = ({ ev: CustomEvent, ): Promise => { widget!.api.transport.reply(ev.detail, {}); - await leaveRTCSession(rtcSession, livekitRoom); + // Only sends matrix leave event. The Livekit session will disconnect once the ActiveCall-view unmounts. + await leaveRTCSession(rtcSession); }; widget.lazyActions.once(ElementWidgetActions.HangupCall, onHangup); return () => { widget!.lazyActions.off(ElementWidgetActions.HangupCall, onHangup); }; } - }, [isJoined, livekitRoom, rtcSession]); + }, [isJoined, rtcSession]); const onReconnect = useCallback(() => { setLeft(false); @@ -326,13 +320,11 @@ export const GroupCallView: FC = ({ /> ); - if (isJoined && livekitRoom) { + if (isJoined) { return ( <> {shareModal} { e2eeConfig: E2EEConfig; } export const ActiveCall: FC = (props) => { - if (!props.livekitRoom) { - return null; - } + const sfuConfig = useOpenIDSFU(props.client, props.rtcSession); + const { livekitRoom, connState } = useLiveKit( + props.rtcSession, + props.muteStates, + sfuConfig, + props.e2eeConfig, + ); + + useEffect(() => { + return () => { + livekitRoom?.disconnect(); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + if (!livekitRoom) return null; return ( - - + + ); }; @@ -193,14 +204,6 @@ export const InCallView: FC = ({ (muted) => muteStates.audio.setEnabled?.(!muted), ); - const onLeavePress = useCallback(() => { - // Disconnect from the room. We don't do this in onLeave because that's - // also called on an unintentional disconnect. Plus we don't have the - // livekit room in onLeave anyway. - livekitRoom.disconnect(); - onLeave(); - }, [livekitRoom, onLeave]); - useEffect(() => { widget?.api.transport.send( layout === "grid" @@ -380,7 +383,9 @@ export const InCallView: FC = ({ buttons.push( , ); diff --git a/src/rtcSessionHelpers.ts b/src/rtcSessionHelpers.ts index 0849c069..2291e80c 100644 --- a/src/rtcSessionHelpers.ts +++ b/src/rtcSessionHelpers.ts @@ -15,7 +15,6 @@ limitations under the License. */ import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession"; -import { Room } from "livekit-client"; import { PosthogAnalytics } from "./analytics/PosthogAnalytics"; import { LivekitFocus } from "./livekit/LivekitFocus"; @@ -69,10 +68,8 @@ const widgetPostHangupProcedure = async ( export async function leaveRTCSession( rtcSession: MatrixRTCSession, - livekitRoom: Room | undefined, ): Promise { await rtcSession.leaveRoomSession(); - await livekitRoom?.disconnect(); if (widget) { await widgetPostHangupProcedure(widget); }