@@ -10,17 +10,13 @@ import { useAgent } from './useAgent';
10
10
import { TrackReferenceOrPlaceholder , TrackReferencePlaceholder } from '@livekit/components-core' ;
11
11
import { useLocalParticipant } from './useLocalParticipant' ;
12
12
13
- /** State representing the current connection status to the server hosted agent */
14
- // FIXME: maybe just make this ConnectionState?
15
- export type AgentSessionConnectionState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'signalReconnecting' ;
16
-
17
13
export enum ConversationEvent {
18
14
ConnectionStateChanged = 'connectionStateChanged' ,
19
15
MediaDevicesError = 'MediaDevicesError' ,
20
16
}
21
17
22
18
export type ConversationCallbacks = {
23
- [ ConversationEvent . ConnectionStateChanged ] : ( newAgentConnectionState : AgentSessionConnectionState ) => void ;
19
+ [ ConversationEvent . ConnectionStateChanged ] : ( newAgentConnectionState : ConnectionState ) => void ;
24
20
[ ConversationEvent . MediaDevicesError ] : ( error : Error ) => void ;
25
21
} ;
26
22
@@ -73,7 +69,7 @@ type ConversationStateCommon = {
73
69
} ;
74
70
75
71
type ConversationStateConnecting = ConversationStateCommon & {
76
- connectionState : "connecting" ;
72
+ connectionState : ConnectionState . Connecting ;
77
73
isConnected : false ;
78
74
isReconnecting : false ;
79
75
@@ -84,7 +80,7 @@ type ConversationStateConnecting = ConversationStateCommon & {
84
80
} ;
85
81
86
82
type ConversationStateConnected = ConversationStateCommon & {
87
- connectionState : "connected" | "reconnecting" | "signalReconnecting" ;
83
+ connectionState : ConnectionState . Connected | ConnectionState . Reconnecting | ConnectionState . SignalReconnecting ;
88
84
isConnected : true ;
89
85
isReconnecting : boolean ;
90
86
@@ -95,7 +91,7 @@ type ConversationStateConnected = ConversationStateCommon & {
95
91
} ;
96
92
97
93
type ConversationStateDisconnected = ConversationStateCommon & {
98
- connectionState : "disconnected" ;
94
+ connectionState : ConnectionState . Disconnected ;
99
95
isConnected : false ;
100
96
isReconnecting : false ;
101
97
@@ -149,20 +145,20 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
149
145
} ;
150
146
} , [ options . tokenSource ] ) ;
151
147
152
- const generateDerivedConnectionStateValues = < ConnectionState extends ConversationInstance [ "connectionState" ] > ( connectionState : ConnectionState ) => ( {
148
+ const generateDerivedConnectionStateValues = useCallback ( < State extends ConversationInstance [ "connectionState" ] > ( connectionState : State ) => ( {
153
149
isConnected : (
154
- connectionState === 'connected' ||
155
- connectionState === 'reconnecting' ||
156
- connectionState === 'signalReconnecting'
150
+ connectionState === ConnectionState . Connected ||
151
+ connectionState === ConnectionState . Reconnecting ||
152
+ connectionState === ConnectionState . SignalReconnecting
157
153
) ,
158
154
isReconnecting : (
159
- connectionState === 'reconnecting' ||
160
- connectionState === 'signalReconnecting'
155
+ connectionState === ConnectionState . Reconnecting ||
156
+ connectionState === ConnectionState . SignalReconnecting
161
157
) ,
162
158
} as {
163
- isConnected : ConnectionState extends 'connected' | 'reconnecting' | 'signalReconnecting' ? true : false ,
164
- isReconnecting : ConnectionState extends 'reconnecting' | 'signalReconnecting' ? true : false ,
165
- } ) ;
159
+ isConnected : State extends ConnectionState . Connected | ConnectionState . Reconnecting | ConnectionState . SignalReconnecting ? true : false ,
160
+ isReconnecting : State extends ConnectionState . Reconnecting | ConnectionState . SignalReconnecting ? true : false ,
161
+ } ) , [ ] ) ;
166
162
167
163
const [ roomConnectionState , setRoomConnectionState ] = useState ( room . state ) ;
168
164
useEffect ( ( ) => {
@@ -214,8 +210,8 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
214
210
return {
215
211
...common ,
216
212
217
- connectionState : 'connecting' ,
218
- ...generateDerivedConnectionStateValues ( 'connecting' ) ,
213
+ connectionState : ConnectionState . Connecting ,
214
+ ...generateDerivedConnectionStateValues ( ConnectionState . Connecting ) ,
219
215
220
216
local : {
221
217
camera : { participant : localParticipant , source : Track . Source . Camera } ,
@@ -251,7 +247,16 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
251
247
} ,
252
248
} ;
253
249
}
254
- } , [ options . tokenSource , room , emitter , roomConnectionState , localParticipant , localCamera , localMicrophone ] ) ;
250
+ } , [
251
+ options . tokenSource ,
252
+ room ,
253
+ emitter ,
254
+ roomConnectionState ,
255
+ localParticipant ,
256
+ localCamera ,
257
+ localMicrophone ,
258
+ generateDerivedConnectionStateValues ,
259
+ ] ) ;
255
260
useEffect ( ( ) => {
256
261
emitter . emit ( ConversationEvent . ConnectionStateChanged , conversationState . connectionState ) ;
257
262
} , [ emitter , conversationState . connectionState ] ) ;
0 commit comments