Skip to content

Commit

Permalink
Don't emit connection errors if connect is set to false (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Nov 18, 2024
1 parent 3e7bd24 commit f7f30bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-glasses-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-react": patch
---

Don't emit connection errors if `connect` is set to false
27 changes: 17 additions & 10 deletions packages/react/src/hooks/useLiveKitRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export function useLiveKitRoom<T extends HTMLElement>(

const [room, setRoom] = React.useState<Room | undefined>();

const shouldConnect = React.useRef(connect);

React.useEffect(() => {
setRoom(passedRoom ?? new Room(options));
}, [passedRoom]);
Expand Down Expand Up @@ -115,23 +117,28 @@ export function useLiveKitRoom<T extends HTMLElement>(
});
return;
}
if (!token) {
log.debug('no token yet');
return;
}
if (!serverUrl) {
log.warn('no livekit url provided');
onError?.(Error('no livekit url provided'));
return;
}

if (connect) {
shouldConnect.current = true;
log.debug('connecting');
if (!token) {
log.debug('no token yet');
return;
}
if (!serverUrl) {
log.warn('no livekit url provided');
onError?.(Error('no livekit url provided'));
return;
}
room.connect(serverUrl, token, connectOptions).catch((e) => {
log.warn(e);
onError?.(e as Error);
if (shouldConnect.current === true) {
onError?.(e as Error);
}
});
} else {
log.debug('disconnecting because connect is false');
shouldConnect.current = false;
room.disconnect();
}
}, [
Expand Down

0 comments on commit f7f30bd

Please sign in to comment.