Skip to content

Commit ce80b15

Browse files
committed
feat: get rid of AgentSessionConnectionState alias, just use existing ConnectionState
1 parent db0104d commit ce80b15

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

packages/react/src/hooks/useConversationWith.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ import { useAgent } from './useAgent';
1010
import { TrackReferenceOrPlaceholder, TrackReferencePlaceholder } from '@livekit/components-core';
1111
import { useLocalParticipant } from './useLocalParticipant';
1212

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-
1713
export enum ConversationEvent {
1814
ConnectionStateChanged = 'connectionStateChanged',
1915
MediaDevicesError = 'MediaDevicesError',
2016
}
2117

2218
export type ConversationCallbacks = {
23-
[ConversationEvent.ConnectionStateChanged]: (newAgentConnectionState: AgentSessionConnectionState) => void;
19+
[ConversationEvent.ConnectionStateChanged]: (newAgentConnectionState: ConnectionState) => void;
2420
[ConversationEvent.MediaDevicesError]: (error: Error) => void;
2521
};
2622

@@ -73,7 +69,7 @@ type ConversationStateCommon = {
7369
};
7470

7571
type ConversationStateConnecting = ConversationStateCommon & {
76-
connectionState: "connecting";
72+
connectionState: ConnectionState.Connecting;
7773
isConnected: false;
7874
isReconnecting: false;
7975

@@ -84,7 +80,7 @@ type ConversationStateConnecting = ConversationStateCommon & {
8480
};
8581

8682
type ConversationStateConnected = ConversationStateCommon & {
87-
connectionState: "connected" | "reconnecting" | "signalReconnecting";
83+
connectionState: ConnectionState.Connected | ConnectionState.Reconnecting | ConnectionState.SignalReconnecting;
8884
isConnected: true;
8985
isReconnecting: boolean;
9086

@@ -95,7 +91,7 @@ type ConversationStateConnected = ConversationStateCommon & {
9591
};
9692

9793
type ConversationStateDisconnected = ConversationStateCommon & {
98-
connectionState: "disconnected";
94+
connectionState: ConnectionState.Disconnected;
9995
isConnected: false;
10096
isReconnecting: false;
10197

@@ -149,20 +145,20 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
149145
};
150146
}, [options.tokenSource]);
151147

152-
const generateDerivedConnectionStateValues = <ConnectionState extends ConversationInstance["connectionState"]>(connectionState: ConnectionState) => ({
148+
const generateDerivedConnectionStateValues = useCallback(<State extends ConversationInstance["connectionState"]>(connectionState: State) => ({
153149
isConnected: (
154-
connectionState === 'connected' ||
155-
connectionState === 'reconnecting' ||
156-
connectionState === 'signalReconnecting'
150+
connectionState === ConnectionState.Connected ||
151+
connectionState === ConnectionState.Reconnecting ||
152+
connectionState === ConnectionState.SignalReconnecting
157153
),
158154
isReconnecting: (
159-
connectionState === 'reconnecting' ||
160-
connectionState === 'signalReconnecting'
155+
connectionState === ConnectionState.Reconnecting ||
156+
connectionState === ConnectionState.SignalReconnecting
161157
),
162158
} 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+
}), []);
166162

167163
const [roomConnectionState, setRoomConnectionState] = useState(room.state);
168164
useEffect(() => {
@@ -214,8 +210,8 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
214210
return {
215211
...common,
216212

217-
connectionState: 'connecting',
218-
...generateDerivedConnectionStateValues('connecting'),
213+
connectionState: ConnectionState.Connecting,
214+
...generateDerivedConnectionStateValues(ConnectionState.Connecting),
219215

220216
local: {
221217
camera: { participant: localParticipant, source: Track.Source.Camera },
@@ -251,7 +247,16 @@ export function useConversationWith(agentToDispatch: string | RoomAgentDispatch
251247
},
252248
};
253249
}
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+
]);
255260
useEffect(() => {
256261
emitter.emit(ConversationEvent.ConnectionStateChanged, conversationState.connectionState);
257262
}, [emitter, conversationState.connectionState]);

0 commit comments

Comments
 (0)