Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-coats-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/components-react': patch
---

Change useAgent to return undefined for nullable TrackReference values
4 changes: 2 additions & 2 deletions packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ import { WidgetState } from '@livekit/components-core';

// @public (undocumented)
export type AgentCallbacks = {
[AgentEvent.CameraChanged]: (newTrack: TrackReference | null) => void;
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference | null) => void;
[AgentEvent.CameraChanged]: (newTrack: TrackReference | undefined) => void;
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference | undefined) => void;
[AgentEvent.StateChanged]: (newAgentState: AgentState) => void;
};

Expand Down
42 changes: 20 additions & 22 deletions packages/react/src/hooks/useAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export enum AgentEvent {

/** @public */
export type AgentCallbacks = {
[AgentEvent.CameraChanged]: (newTrack: TrackReference | null) => void;
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference | null) => void;
[AgentEvent.CameraChanged]: (newTrack: TrackReference | undefined) => void;
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference | undefined) => void;
[AgentEvent.StateChanged]: (newAgentState: AgentState) => void;
};

Expand All @@ -79,8 +79,8 @@ type AgentStateAvailable = AgentStateCommon & {
* connected yet? */
isBufferingSpeech: false;

cameraTrack: TrackReference | null;
microphoneTrack: TrackReference | null;
cameraTrack?: TrackReference;
microphoneTrack?: TrackReference;
};

type AgentStateAvailableListening = AgentStateCommon & {
Expand All @@ -94,8 +94,8 @@ type AgentStateAvailableListening = AgentStateCommon & {
* connected yet? */
isBufferingSpeech: boolean;

cameraTrack: TrackReference | null;
microphoneTrack: TrackReference | null;
cameraTrack?: TrackReference;
microphoneTrack?: TrackReference;
};

type AgentStateUnAvailable = AgentStateCommon & {
Expand All @@ -109,8 +109,8 @@ type AgentStateUnAvailable = AgentStateCommon & {
* connected yet? */
isBufferingSpeech: false;

cameraTrack: TrackReference | null;
microphoneTrack: TrackReference | null;
cameraTrack?: TrackReference;
microphoneTrack?: TrackReference;
};

type AgentStateConnecting = AgentStateCommon & {
Expand All @@ -124,8 +124,8 @@ type AgentStateConnecting = AgentStateCommon & {
* connected yet? */
isBufferingSpeech: false;

cameraTrack: null;
microphoneTrack: null;
cameraTrack: undefined;
microphoneTrack: undefined;
};

type AgentStateFailed = AgentStateCommon & {
Expand All @@ -139,8 +139,8 @@ type AgentStateFailed = AgentStateCommon & {
* connected yet? */
isBufferingSpeech: false;

cameraTrack: null;
microphoneTrack: null;
cameraTrack: undefined;
microphoneTrack: undefined;
};

type AgentActions = {
Expand Down Expand Up @@ -325,8 +325,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
const videoTrack = React.useMemo(
() =>
agentTracks.find((t) => t.source === Track.Source.Camera) ??
workerTracks.find((t) => t.source === Track.Source.Camera) ??
null,
workerTracks.find((t) => t.source === Track.Source.Camera),
[agentTracks, workerTracks],
);
React.useEffect(() => {
Expand All @@ -336,8 +335,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
const audioTrack = React.useMemo(
() =>
agentTracks.find((t) => t.source === Track.Source.Microphone) ??
workerTracks.find((t) => t.source === Track.Source.Microphone) ??
null,
workerTracks.find((t) => t.source === Track.Source.Microphone),
[agentTracks, workerTracks],
);
React.useEffect(() => {
Expand Down Expand Up @@ -468,8 +466,8 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
failureReasons: null,

// Clear inner values if no longer connected
cameraTrack: null,
microphoneTrack: null,
cameraTrack: undefined,
microphoneTrack: undefined,
};
Comment on lines 468 to 471
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to call out that I kept these key: undefined type entries here so that destructuring would continue to work coming out of useAgent - ie, const { cameraTrack, microphoneTrack } = useAgent(session). Otherwise this pattern would break down because cameraTrack + microphoneTrack wouldn't always be in the return value.


case 'initializing':
Expand Down Expand Up @@ -523,8 +521,8 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
failureReasons,

// Clear inner values if no longer connected
cameraTrack: null,
microphoneTrack: null,
cameraTrack: undefined,
microphoneTrack: undefined,
};
}
}, [
Expand Down Expand Up @@ -574,7 +572,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
const waitUntilCamera = React.useCallback(
(signal?: AbortSignal) => {
return new Promise<TrackReference>((resolve, reject) => {
const stateChangedHandler = (camera: TrackReference | null) => {
const stateChangedHandler = (camera: TrackReference | undefined) => {
if (!camera) {
return;
}
Expand All @@ -601,7 +599,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
const waitUntilMicrophone = React.useCallback(
(signal?: AbortSignal) => {
return new Promise<TrackReference>((resolve, reject) => {
const stateChangedHandler = (microphone: TrackReference | null) => {
const stateChangedHandler = (microphone: TrackReference | undefined) => {
if (!microphone) {
return;
}
Expand Down