Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
Fix disconnect on unmount regression due to strict mode (#112)
Browse files Browse the repository at this point in the history
* Fix disconnect on onmount regression due to strict mode

* changeset
  • Loading branch information
lukasIO authored Dec 21, 2022
1 parent 17009e8 commit 0273d9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
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

0 comments on commit 0273d9e

Please sign in to comment.