Skip to content

Commit

Permalink
Forward disconnectReason to onDisconnected callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Nov 21, 2024
1 parent 92e2c17 commit ed616f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/react/src/components/LiveKitRoom.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
AudioCaptureOptions,
DisconnectReason,
RoomConnectOptions,
RoomOptions,
ScreenShareCaptureOptions,
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface LiveKitRoomProps extends Omit<React.HTMLAttributes<HTMLDivEleme
*/
connectOptions?: RoomConnectOptions;
onConnected?: () => void;
onDisconnected?: () => void;
onDisconnected?: (reason?: DisconnectReason) => void;
onError?: (error: Error) => void;
onMediaDeviceFailure?: (failure?: MediaDeviceFailure) => void;
onEncryptionError?: (error: Error) => void;
Expand Down
39 changes: 15 additions & 24 deletions packages/react/src/hooks/useLiveKitRoom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { log, setupLiveKitRoom } from '@livekit/components-core';
import { Room, MediaDeviceFailure, RoomEvent, ConnectionState } from 'livekit-client';
import type { DisconnectReason } from 'livekit-client';
import { Room, MediaDeviceFailure, RoomEvent } from 'livekit-client';
import * as React from 'react';
import type { HTMLAttributes } from 'react';

Expand Down Expand Up @@ -89,16 +90,27 @@ export function useLiveKitRoom<T extends HTMLElement>(
const handleEncryptionError = (e: Error) => {
onEncryptionError?.(e);
};
const handleDisconnected = (reason?: DisconnectReason) => {
onDisconnected?.(reason);
};
const handleConnected = () => {
onConnected?.();
};

room
.on(RoomEvent.SignalConnected, onSignalConnected)
.on(RoomEvent.MediaDevicesError, handleMediaDeviceError)
.on(RoomEvent.EncryptionError, handleEncryptionError);
.on(RoomEvent.EncryptionError, handleEncryptionError)
.on(RoomEvent.Disconnected, handleDisconnected)
.on(RoomEvent.Connected, handleConnected);

return () => {
room
.off(RoomEvent.SignalConnected, onSignalConnected)
.off(RoomEvent.MediaDevicesError, handleMediaDeviceError)
.off(RoomEvent.EncryptionError, handleEncryptionError);
.off(RoomEvent.EncryptionError, handleEncryptionError)
.off(RoomEvent.Disconnected, handleDisconnected)
.off(RoomEvent.Connected, handleConnected);
};
}, [room, audio, video, screen, onError, onEncryptionError, onMediaDeviceFailure]);

Expand Down Expand Up @@ -151,27 +163,6 @@ export function useLiveKitRoom<T extends HTMLElement>(
simulateParticipants,
]);

React.useEffect(() => {
if (!room) return;
const connectionStateChangeListener = (state: ConnectionState) => {
switch (state) {
case ConnectionState.Disconnected:
if (onDisconnected) onDisconnected();
break;
case ConnectionState.Connected:
if (onConnected) onConnected();
break;

default:
break;
}
};
room.on(RoomEvent.ConnectionStateChanged, connectionStateChangeListener);
return () => {
room.off(RoomEvent.ConnectionStateChanged, connectionStateChangeListener);
};
}, [token, onConnected, onDisconnected, room]);

React.useEffect(() => {
if (!room) return;
return () => {
Expand Down

0 comments on commit ed616f8

Please sign in to comment.