Skip to content

Commit f7f30bd

Browse files
authored
Don't emit connection errors if connect is set to false (#1025)
1 parent 3e7bd24 commit f7f30bd

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

.changeset/new-glasses-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/components-react": patch
3+
---
4+
5+
Don't emit connection errors if `connect` is set to false

packages/react/src/hooks/useLiveKitRoom.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export function useLiveKitRoom<T extends HTMLElement>(
5555

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

58+
const shouldConnect = React.useRef(connect);
59+
5860
React.useEffect(() => {
5961
setRoom(passedRoom ?? new Room(options));
6062
}, [passedRoom]);
@@ -115,23 +117,28 @@ export function useLiveKitRoom<T extends HTMLElement>(
115117
});
116118
return;
117119
}
118-
if (!token) {
119-
log.debug('no token yet');
120-
return;
121-
}
122-
if (!serverUrl) {
123-
log.warn('no livekit url provided');
124-
onError?.(Error('no livekit url provided'));
125-
return;
126-
}
120+
127121
if (connect) {
122+
shouldConnect.current = true;
128123
log.debug('connecting');
124+
if (!token) {
125+
log.debug('no token yet');
126+
return;
127+
}
128+
if (!serverUrl) {
129+
log.warn('no livekit url provided');
130+
onError?.(Error('no livekit url provided'));
131+
return;
132+
}
129133
room.connect(serverUrl, token, connectOptions).catch((e) => {
130134
log.warn(e);
131-
onError?.(e as Error);
135+
if (shouldConnect.current === true) {
136+
onError?.(e as Error);
137+
}
132138
});
133139
} else {
134140
log.debug('disconnecting because connect is false');
141+
shouldConnect.current = false;
135142
room.disconnect();
136143
}
137144
}, [

0 commit comments

Comments
 (0)