mirror of
https://github.com/vector-im/element-call.git
synced 2026-08-29 21:15:19 +00:00
Keep useActiveLivekitFocus from changing focus spuriously
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
|||||||
type MatrixRTCSession,
|
type MatrixRTCSession,
|
||||||
MatrixRTCSessionEvent,
|
MatrixRTCSessionEvent,
|
||||||
} from "matrix-js-sdk/lib/matrixrtc";
|
} from "matrix-js-sdk/lib/matrixrtc";
|
||||||
import { useCallback, useEffect, useRef } from "react";
|
import { useCallback, useRef } from "react";
|
||||||
import { deepCompare } from "matrix-js-sdk/lib/utils";
|
import { deepCompare } from "matrix-js-sdk/lib/utils";
|
||||||
import { logger } from "matrix-js-sdk/lib/logger";
|
import { logger } from "matrix-js-sdk/lib/logger";
|
||||||
import { type LivekitFocus, isLivekitFocus } from "matrix-js-sdk/lib/matrixrtc";
|
import { type LivekitFocus, isLivekitFocus } from "matrix-js-sdk/lib/matrixrtc";
|
||||||
@@ -24,27 +24,22 @@ import { useTypedEventEmitterState } from "../useEvents";
|
|||||||
export function useActiveLivekitFocus(
|
export function useActiveLivekitFocus(
|
||||||
rtcSession: MatrixRTCSession,
|
rtcSession: MatrixRTCSession,
|
||||||
): LivekitFocus | undefined {
|
): LivekitFocus | undefined {
|
||||||
const activeFocus = useTypedEventEmitterState(
|
const prevActiveFocus = useRef<LivekitFocus | undefined>(undefined);
|
||||||
|
return useTypedEventEmitterState(
|
||||||
rtcSession,
|
rtcSession,
|
||||||
MatrixRTCSessionEvent.MembershipsChanged,
|
MatrixRTCSessionEvent.MembershipsChanged,
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
const f = rtcSession.getActiveFocus();
|
const f = rtcSession.getActiveFocus();
|
||||||
// Only handle foci with type="livekit" for now.
|
// Only handle foci with type="livekit" for now.
|
||||||
return !!f && isLivekitFocus(f) ? f : undefined;
|
if (f && isLivekitFocus(f) && !deepCompare(f, prevActiveFocus.current)) {
|
||||||
|
const oldestMembership = rtcSession.getOldestMembership();
|
||||||
|
logger.info(
|
||||||
|
`Got new active focus from membership: ${oldestMembership?.sender}/${oldestMembership?.deviceId}.
|
||||||
|
Updated focus (focus switch) from ${JSON.stringify(prevActiveFocus.current)} to ${JSON.stringify(f)}`,
|
||||||
|
);
|
||||||
|
prevActiveFocus.current = f;
|
||||||
|
}
|
||||||
|
return prevActiveFocus.current;
|
||||||
}, [rtcSession]),
|
}, [rtcSession]),
|
||||||
);
|
);
|
||||||
|
|
||||||
const prevActiveFocus = useRef(activeFocus);
|
|
||||||
useEffect(() => {
|
|
||||||
if (!deepCompare(activeFocus, prevActiveFocus.current)) {
|
|
||||||
const oldestMembership = rtcSession.getOldestMembership();
|
|
||||||
logger.warn(
|
|
||||||
`Got new active focus from membership: ${oldestMembership?.sender}/${oldestMembership?.deviceId}.
|
|
||||||
Updated focus (focus switch) from ${JSON.stringify(prevActiveFocus.current)} to ${JSON.stringify(activeFocus)}`,
|
|
||||||
);
|
|
||||||
prevActiveFocus.current = activeFocus;
|
|
||||||
}
|
|
||||||
}, [activeFocus, rtcSession]);
|
|
||||||
|
|
||||||
return activeFocus;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user