Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/real-pants-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@livekit/react-components': minor
'@livekit/react-core': minor
---

Breaking: useRoom().room could be undefined on first render! Fix auto-disconnect when LiveKitRoom component unmounts"
23 changes: 12 additions & 11 deletions packages/components/src/LiveKitRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ export const LiveKitRoom = ({
const roomState = useRoom(roomOptions);

useEffect(() => {
roomState.connect(url, token, connectOptions).then((room) => {
if (!room) {
return;
}
if (onConnected && room.state === ConnectionState.Connected) {
onConnected(room);
}
});

if (roomState.room) {
roomState.connect(url, token, connectOptions).then((room) => {
if (!room) {
return;
}
if (onConnected && room.state === ConnectionState.Connected) {
onConnected(room);
}
});
}
return () => {
if (roomState.connectionState !== ConnectionState.Disconnected) {
if (roomState.room?.state !== ConnectionState.Disconnected) {
roomState.room?.disconnect();
}
};
}, []);
}, [roomState.room]);

const selectedStageRenderer = stageRenderer ?? StageView;

Expand Down
19 changes: 14 additions & 5 deletions packages/core/src/hooks/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RoomConnectOptions,
ConnectionState,
} from 'livekit-client';
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

export interface RoomState {
connect: (url: string, token: string, options?: RoomConnectOptions) => Promise<Room | undefined>;
Expand All @@ -24,7 +24,7 @@ export interface RoomState {
}

export function useRoom(roomOptions?: RoomOptions): RoomState {
const [room] = useState<Room>(new Room(roomOptions));
const [room, setRoom] = useState<Room | undefined>();
const [isConnecting, setIsConnecting] = useState(false);
const [error, setError] = useState<Error>();
const [participants, setParticipants] = useState<Participant[]>([]);
Expand All @@ -33,6 +33,10 @@ export function useRoom(roomOptions?: RoomOptions): RoomState {
ConnectionState.Disconnected,
);

useEffect(() => {
setRoom(new Room(roomOptions));
}, []);

const connectFn = useCallback(
async (url: string, token: string, options?: RoomConnectOptions) => {
setIsConnecting(true);
Expand Down Expand Up @@ -65,6 +69,11 @@ export function useRoom(roomOptions?: RoomOptions): RoomState {
setConnectionState(state);
};

if (!room) {
setError(new Error('room is not ready yet'));
return;
}

room.once(RoomEvent.Disconnected, () => {
room
.off(RoomEvent.ParticipantConnected, onParticipantsChanged)
Expand All @@ -89,7 +98,7 @@ export function useRoom(roomOptions?: RoomOptions): RoomState {
.on(RoomEvent.AudioPlaybackStatusChanged, onParticipantsChanged)
.on(RoomEvent.ConnectionStateChanged, onConnectionStateChanged);

await room?.connect(url, token, options);
await room.connect(url, token, options);
setIsConnecting(false);
onSubscribedTrackChanged();
setError(undefined);
Expand All @@ -105,13 +114,13 @@ export function useRoom(roomOptions?: RoomOptions): RoomState {
return undefined;
}
},
[],
[room],
);

return {
connect: connectFn,
isConnecting,
room: room,
room,
error,
participants,
audioTracks,
Expand Down