|
1 | 1 | import { log, setupLiveKitRoom } from '@livekit/components-core';
|
2 |
| -import { Room, MediaDeviceFailure, RoomEvent, ConnectionState } from 'livekit-client'; |
| 2 | +import type { DisconnectReason } from 'livekit-client'; |
| 3 | +import { Room, MediaDeviceFailure, RoomEvent } from 'livekit-client'; |
3 | 4 | import * as React from 'react';
|
4 | 5 | import type { HTMLAttributes } from 'react';
|
5 | 6 |
|
@@ -89,18 +90,39 @@ export function useLiveKitRoom<T extends HTMLElement>(
|
89 | 90 | const handleEncryptionError = (e: Error) => {
|
90 | 91 | onEncryptionError?.(e);
|
91 | 92 | };
|
| 93 | + const handleDisconnected = (reason?: DisconnectReason) => { |
| 94 | + onDisconnected?.(reason); |
| 95 | + }; |
| 96 | + const handleConnected = () => { |
| 97 | + onConnected?.(); |
| 98 | + }; |
| 99 | + |
92 | 100 | room
|
93 | 101 | .on(RoomEvent.SignalConnected, onSignalConnected)
|
94 | 102 | .on(RoomEvent.MediaDevicesError, handleMediaDeviceError)
|
95 |
| - .on(RoomEvent.EncryptionError, handleEncryptionError); |
| 103 | + .on(RoomEvent.EncryptionError, handleEncryptionError) |
| 104 | + .on(RoomEvent.Disconnected, handleDisconnected) |
| 105 | + .on(RoomEvent.Connected, handleConnected); |
96 | 106 |
|
97 | 107 | return () => {
|
98 | 108 | room
|
99 | 109 | .off(RoomEvent.SignalConnected, onSignalConnected)
|
100 | 110 | .off(RoomEvent.MediaDevicesError, handleMediaDeviceError)
|
101 |
| - .off(RoomEvent.EncryptionError, handleEncryptionError); |
| 111 | + .off(RoomEvent.EncryptionError, handleEncryptionError) |
| 112 | + .off(RoomEvent.Disconnected, handleDisconnected) |
| 113 | + .off(RoomEvent.Connected, handleConnected); |
102 | 114 | };
|
103 |
| - }, [room, audio, video, screen, onError, onEncryptionError, onMediaDeviceFailure]); |
| 115 | + }, [ |
| 116 | + room, |
| 117 | + audio, |
| 118 | + video, |
| 119 | + screen, |
| 120 | + onError, |
| 121 | + onEncryptionError, |
| 122 | + onMediaDeviceFailure, |
| 123 | + onConnected, |
| 124 | + onDisconnected, |
| 125 | + ]); |
104 | 126 |
|
105 | 127 | React.useEffect(() => {
|
106 | 128 | if (!room) return;
|
@@ -151,27 +173,6 @@ export function useLiveKitRoom<T extends HTMLElement>(
|
151 | 173 | simulateParticipants,
|
152 | 174 | ]);
|
153 | 175 |
|
154 |
| - React.useEffect(() => { |
155 |
| - if (!room) return; |
156 |
| - const connectionStateChangeListener = (state: ConnectionState) => { |
157 |
| - switch (state) { |
158 |
| - case ConnectionState.Disconnected: |
159 |
| - if (onDisconnected) onDisconnected(); |
160 |
| - break; |
161 |
| - case ConnectionState.Connected: |
162 |
| - if (onConnected) onConnected(); |
163 |
| - break; |
164 |
| - |
165 |
| - default: |
166 |
| - break; |
167 |
| - } |
168 |
| - }; |
169 |
| - room.on(RoomEvent.ConnectionStateChanged, connectionStateChangeListener); |
170 |
| - return () => { |
171 |
| - room.off(RoomEvent.ConnectionStateChanged, connectionStateChangeListener); |
172 |
| - }; |
173 |
| - }, [token, onConnected, onDisconnected, room]); |
174 |
| - |
175 | 176 | React.useEffect(() => {
|
176 | 177 | if (!room) return;
|
177 | 178 | return () => {
|
|
0 commit comments