From b256755a0d98ee1d0d931fc8060fdcb397347212 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 31 Aug 2023 09:44:23 +0100 Subject: [PATCH] Don't treat empty object as a valid sfu config This was causing an extra reconnect cycle when the call was first joined because it thought the previous SFU config was valid. This was probably causing some client to fail to connect at all. --- src/livekit/useECConnectionState.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/livekit/useECConnectionState.ts b/src/livekit/useECConnectionState.ts index 2e8bb430..01adb64d 100644 --- a/src/livekit/useECConnectionState.ts +++ b/src/livekit/useECConnectionState.ts @@ -36,6 +36,12 @@ export enum ECAddonConnectionState { export type ECConnectionState = ConnectionState | ECAddonConnectionState; +// This is mostly necessary because an empty useRef is an empty object +// which is truthy, so we can't just use Boolean(currentSFUConfig.current) +function sfuConfigValid(sfuConfig?: SFUConfig): boolean { + return Boolean(sfuConfig?.url) && Boolean(sfuConfig?.jwt); +} + export function useECConnectionState( livekitRoom?: Room, sfuConfig?: SFUConfig @@ -68,18 +74,22 @@ export function useECConnectionState( const currentSFUConfig = useRef(Object.assign({}, sfuConfig)); + // Id we are transitioning from a valid config to another valid one, we need + // to explicitly switch focus useEffect(() => { if ( - sfuConfig && - currentSFUConfig.current && + sfuConfigValid(sfuConfig) && + sfuConfigValid(currentSFUConfig.current) && !sfuConfigEquals(currentSFUConfig.current, sfuConfig) ) { - logger.info("JWT changed!"); + logger.info( + `SFU config changed! URL was ${currentSFUConfig.current?.url} now ${sfuConfig?.url}` + ); (async () => { setSwitchingFocus(true); await livekitRoom?.disconnect(); - await livekitRoom?.connect(sfuConfig.url, sfuConfig.jwt); + await livekitRoom?.connect(sfuConfig!.url, sfuConfig!.jwt); })(); }