From 25efc1cb46c314a3f654da7d83a755101998b413 Mon Sep 17 00:00:00 2001 From: Timo K Date: Tue, 28 Nov 2023 14:34:45 +0100 Subject: [PATCH] Refactor livekit disconnect to use an effect hook. Signed-off-by: Timo K --- src/room/GroupCallView.tsx | 24 ++++++++---------------- src/room/InCallView.tsx | 36 +++++++++++++++++++++++++----------- src/rtcSessionHelpers.ts | 3 --- 3 files changed, 33 insertions(+), 30 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, + ); + + const cleanup = useCallback(() => { + if (connState === ConnectionState.Connected) { + livekitRoom?.disconnect(); + } + }, [livekitRoom, connState]); + + useEffect(() => { + return cleanup; + }, [cleanup, livekitRoom]); + + if (!livekitRoom) return null; return ( - - + + ); }; diff --git a/src/rtcSessionHelpers.ts b/src/rtcSessionHelpers.ts index 3f7d8492..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,9 +68,7 @@ const widgetPostHangupProcedure = async ( export async function leaveRTCSession( rtcSession: MatrixRTCSession, - livekitRoom: Room | undefined, ): Promise { - await livekitRoom?.disconnect(); await rtcSession.leaveRoomSession(); if (widget) { await widgetPostHangupProcedure(widget);