Skip to content

Commit 6d8c8c5

Browse files
committed
feat: remove track reference generic logic, at least for now
I want to make this review process smoother and while I think this is a really good idea, it's not directly related to the core of the change.
1 parent e295967 commit 6d8c8c5

File tree

8 files changed

+47
-62
lines changed

8 files changed

+47
-62
lines changed

packages/core/src/observables/track.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { allParticipantRoomEvents, participantTrackEvents } from '../helper';
1212
import { log } from '../logger';
1313
import type { TrackReference } from '../track-reference';
1414
import { observeRoomEvents } from './room';
15-
import type { ExtractTrackSourcesFromParticipantTrackIdentifier, ParticipantTrackIdentifier } from '../types';
15+
import type { ParticipantTrackIdentifier } from '../types';
1616
import { observeParticipantEvents } from './participant';
1717
// @ts-ignore some module resolutions (other than 'node') choke on this
1818
import type { PublicationEventCallbacks } from 'livekit-client/dist/src/room/track/TrackPublication';
@@ -93,15 +93,11 @@ function getTrackReferences(
9393
/**
9494
* Create `TrackReferences` for all tracks that are included in the sources property.
9595
* */
96-
function getParticipantTrackRefs<
97-
TrackSource extends ExtractTrackSourcesFromParticipantTrackIdentifier<TrackIdentifier>,
98-
P extends Participant,
99-
TrackIdentifier extends ParticipantTrackIdentifier = ParticipantTrackIdentifier,
100-
>(
101-
participant: P,
102-
identifier: TrackIdentifier,
96+
function getParticipantTrackRefs(
97+
participant: Participant,
98+
identifier: ParticipantTrackIdentifier,
10399
onlySubscribedTracks = false,
104-
): Array<TrackReference<TrackSource, P>> {
100+
): TrackReference[] {
105101
const { sources, kind, name } = identifier;
106102
const sourceReferences = Array.from(participant.trackPublications.values())
107103
.filter(
@@ -112,11 +108,11 @@ function getParticipantTrackRefs<
112108
// either return all or only the ones that are subscribed
113109
(!onlySubscribedTracks || pub.track),
114110
)
115-
.map((track): TrackReference<TrackSource, P> => {
111+
.map((track): TrackReference => {
116112
return {
117113
participant: participant,
118114
publication: track,
119-
source: track.source as TrackSource,
115+
source: track.source,
120116
};
121117
});
122118

@@ -161,21 +157,17 @@ export function trackReferencesObservable(
161157
return observable;
162158
}
163159

164-
export function participantTracksObservable<
165-
TrackSource extends ExtractTrackSourcesFromParticipantTrackIdentifier<TrackIdentifier>,
166-
P extends Participant = Participant,
167-
TrackIdentifier extends ParticipantTrackIdentifier = ParticipantTrackIdentifier,
168-
>(
169-
participant: P,
170-
trackIdentifier: TrackIdentifier,
171-
): Observable<Array<TrackReference<TrackSource, P>>> {
160+
export function participantTracksObservable(
161+
participant: Participant,
162+
trackIdentifier: ParticipantTrackIdentifier,
163+
): Observable<TrackReference[]> {
172164
const observable = observeParticipantEvents(participant, ...participantTrackEvents).pipe(
173165
map((participant) => {
174-
const data = getParticipantTrackRefs<TrackSource, P>(participant, trackIdentifier);
166+
const data = getParticipantTrackRefs(participant, trackIdentifier);
175167
log.debug(`TrackReference[] was updated. (length ${data.length})`, data);
176168
return data;
177169
}),
178-
startWith(getParticipantTrackRefs<TrackSource, P>(participant, trackIdentifier)),
170+
startWith(getParticipantTrackRefs(participant, trackIdentifier)),
179171
);
180172

181173
return observable;

packages/core/src/track-reference/track-reference.types.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ import type { Participant, Track, TrackPublication } from 'livekit-client';
77
// ## TrackReference Types
88

99
/** @public */
10-
export type TrackReferencePlaceholder<TrackSource extends Track.Source = Track.Source, P = Participant> = {
11-
participant: P;
10+
export type TrackReferencePlaceholder = {
11+
participant: Participant;
1212
publication?: never;
13-
source: TrackSource;
13+
source: Track.Source;
1414
};
1515

1616
/** @public */
17-
export type TrackReference<TrackSource extends Track.Source = Track.Source, P = Participant> = {
18-
participant: P;
17+
export type TrackReference = {
18+
participant: Participant;
1919
publication: TrackPublication;
20-
source: TrackSource;
20+
source: Track.Source;
2121
};
2222

2323
/** @public */
24-
export type TrackReferenceOrPlaceholder<
25-
TrackSource extends Track.Source = Track.Source,
26-
P = Participant,
27-
> = TrackReference<TrackSource, P> | TrackReferencePlaceholder<TrackSource, P>;
24+
export type TrackReferenceOrPlaceholder = TrackReference | TrackReferencePlaceholder;
2825

2926
// ### TrackReference Type Predicates
3027
/** @internal */

packages/core/src/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ export type ParticipantTrackIdentifier = RequireAtLeastOne<
5757
'sources' | 'name' | 'kind'
5858
>;
5959

60-
export type ExtractTrackSourcesFromParticipantTrackIdentifier<TrackIdentifier extends ParticipantTrackIdentifier> = (
61-
TrackIdentifier['sources'] extends undefined ? Track.Source : NonNullable<ParticipantTrackIdentifier['sources']>[0]
62-
);
63-
6460
/**
6561
* @beta
6662
*/

packages/react/src/hooks/useAgent.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export enum AgentEvent {
2424
}
2525

2626
export type AgentCallbacks = {
27-
[AgentEvent.CameraChanged]: (newTrack: TrackReference<Track.Source.Camera, RemoteParticipant> | null) => void;
28-
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference<Track.Source.Microphone, RemoteParticipant> | null) => void;
27+
[AgentEvent.CameraChanged]: (newTrack: TrackReference | null) => void;
28+
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference | null) => void;
2929
[AgentEvent.AttributesChanged]: (newAttributes: Record<string, string>) => void;
3030
[AgentEvent.StateChanged]: (newAgentState: AgentStateNew) => void;
3131
};
@@ -56,8 +56,8 @@ type AgentStateAvailable = AgentInstanceCommon & {
5656
/** Is the agent ready for user interaction? */
5757
isAvailable: true;
5858

59-
camera: TrackReference<Track.Source.Camera, RemoteParticipant> | null;
60-
microphone: TrackReference<Track.Source.Microphone, RemoteParticipant> | null;
59+
camera: TrackReference | null;
60+
microphone: TrackReference | null;
6161
};
6262

6363
type AgentStateUnAvailable = AgentInstanceCommon & {
@@ -87,10 +87,10 @@ type AgentActions = {
8787
waitUntilAvailable: (signal?: AbortSignal) => Promise<void>;
8888

8989
/** Returns a promise that resolves once the agent has published a camera track */
90-
waitUntilCamera: (signal?: AbortSignal) => Promise<TrackReference<Track.Source.Camera, RemoteParticipant>>;
90+
waitUntilCamera: (signal?: AbortSignal) => Promise<TrackReference>;
9191

9292
/** Returns a promise that resolves once the agent has published a microphone track */
93-
waitUntilMicrophone: (signal?: AbortSignal) => Promise<TrackReference<Track.Source.Microphone, RemoteParticipant>>;
93+
waitUntilMicrophone: (signal?: AbortSignal) => Promise<TrackReference>;
9494
};
9595

9696
type AgentStateCases = AgentStateAvailable | AgentStateUnAvailable | AgentStateFailed;
@@ -244,15 +244,15 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
244244
const videoTrack = useMemo(() => (
245245
agentTracks.find((t) => t.source === Track.Source.Camera) ??
246246
workerTracks.find((t) => t.source === Track.Source.Camera) ?? null
247-
) as TrackReference<Track.Source.Camera, RemoteParticipant> | null, [agentTracks, workerTracks]);
247+
), [agentTracks, workerTracks]);
248248
useEffect(() => {
249249
emitter.emit(AgentEvent.CameraChanged, videoTrack);
250250
}, [emitter, videoTrack]);
251251

252252
const audioTrack = useMemo(() => (
253253
agentTracks.find((t) => t.source === Track.Source.Microphone) ??
254254
workerTracks.find((t) => t.source === Track.Source.Microphone) ?? null
255-
) as TrackReference<Track.Source.Microphone, RemoteParticipant> | null, [agentTracks, workerTracks]);
255+
), [agentTracks, workerTracks]);
256256
useEffect(() => {
257257
emitter.emit(AgentEvent.MicrophoneChanged, audioTrack);
258258
}, [emitter, audioTrack]);
@@ -470,8 +470,8 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
470470
}, [state, emitter]);
471471

472472
const waitUntilCamera = useCallback((signal?: AbortSignal) => {
473-
return new Promise<TrackReference<Track.Source.Camera, RemoteParticipant>>((resolve, reject) => {
474-
const stateChangedHandler = (camera: TrackReference<Track.Source.Camera, RemoteParticipant> | null) => {
473+
return new Promise<TrackReference>((resolve, reject) => {
474+
const stateChangedHandler = (camera: TrackReference | null) => {
475475
if (!camera) {
476476
return;
477477
}
@@ -494,8 +494,8 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
494494
}, [emitter]);
495495

496496
const waitUntilMicrophone = useCallback((signal?: AbortSignal) => {
497-
return new Promise<TrackReference<Track.Source.Microphone, RemoteParticipant>>((resolve, reject) => {
498-
const stateChangedHandler = (microphone: TrackReference<Track.Source.Microphone, RemoteParticipant> | null) => {
497+
return new Promise<TrackReference>((resolve, reject) => {
498+
const stateChangedHandler = (microphone: TrackReference | null) => {
499499
if (!microphone) {
500500
return;
501501
}

packages/react/src/hooks/useConversationWith.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type TypedEventEmitter from 'typed-emitter';
2-
import { Room, RoomEvent, ConnectionState, TrackPublishOptions, Track, LocalParticipant } from 'livekit-client';
2+
import { Room, RoomEvent, ConnectionState, TrackPublishOptions, Track } from 'livekit-client';
33
import { EventEmitter } from 'events';
44
import { useCallback, useEffect, useMemo, useState } from 'react';
55

@@ -78,8 +78,8 @@ type ConversationStateConnecting = ConversationStateCommon & {
7878
isReconnecting: false;
7979

8080
local: {
81-
camera: TrackReferencePlaceholder<Track.Source.Camera, LocalParticipant>;
82-
microphone: TrackReferencePlaceholder<Track.Source.Microphone, LocalParticipant>;
81+
camera: TrackReferencePlaceholder;
82+
microphone: TrackReferencePlaceholder;
8383
};
8484
};
8585

@@ -89,8 +89,8 @@ type ConversationStateConnected = ConversationStateCommon & {
8989
isReconnecting: boolean;
9090

9191
local: {
92-
camera: TrackReferenceOrPlaceholder<Track.Source.Camera, LocalParticipant>,
93-
microphone: TrackReferenceOrPlaceholder<Track.Source.Microphone, LocalParticipant>,
92+
camera: TrackReferenceOrPlaceholder,
93+
microphone: TrackReferenceOrPlaceholder,
9494
};
9595
};
9696

@@ -100,8 +100,8 @@ type ConversationStateDisconnected = ConversationStateCommon & {
100100
isReconnecting: false;
101101

102102
local: {
103-
camera: TrackReferencePlaceholder<Track.Source.Camera, LocalParticipant>;
104-
microphone: TrackReferencePlaceholder<Track.Source.Microphone, LocalParticipant>;
103+
camera: TrackReferencePlaceholder;
104+
microphone: TrackReferencePlaceholder;
105105
};
106106
};
107107

packages/react/src/hooks/useParticipantTracks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type UseParticipantTracksOptions = {
1919
export function useParticipantTracks<TrackSource extends Track.Source>(
2020
sources: Array<TrackSource>,
2121
optionsOrParticipantIdentity: UseParticipantTracksOptions | UseParticipantTracksOptions["participantIdentity"] = {},
22-
): Array<TrackReference<TrackSource>> {
22+
): Array<TrackReference> {
2323
let participantIdentity: UseParticipantTracksOptions["participantIdentity"];
2424
let room: UseParticipantTracksOptions["room"];
2525
if (typeof optionsOrParticipantIdentity === 'string') {
@@ -46,7 +46,7 @@ export function useParticipantTracks<TrackSource extends Track.Source>(
4646
return participantTracksObservable<TrackSource>(p, { sources });
4747
}, [p, JSON.stringify(sources)]);
4848

49-
const trackRefs = useObservableState(observable, [] as Array<TrackReference<TrackSource>>);
49+
const trackRefs = useObservableState(observable, [] as Array<TrackReference>);
5050

5151
return trackRefs;
5252
}

packages/react/src/hooks/useTrackRefBySourceOrName.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { Track } from 'livekit-client';
66
/**
77
* @internal
88
*/
9-
export function useTrackRefBySourceOrName<Source extends TrackSource<TS>, TS extends Track.Source>(
10-
source: Source,
11-
): Source['source'] extends undefined ? TrackReferenceOrPlaceholder : TrackReferenceOrPlaceholder<TS> {
9+
export function useTrackRefBySourceOrName(
10+
source: TrackSource<Track.Source>,
11+
): TrackReferenceOrPlaceholder {
1212
const [publication, setPublication] = React.useState(getTrackByIdentifier(source));
1313

1414
const { trackObserver } = React.useMemo(() => {
@@ -24,7 +24,7 @@ export function useTrackRefBySourceOrName<Source extends TrackSource<TS>, TS ext
2424

2525
return {
2626
participant: source.participant,
27-
source: (source.source ?? Track.Source.Unknown) as TS,
27+
source: source.source ?? Track.Source.Unknown,
2828
publication,
2929
};
3030
}

packages/react/src/hooks/useTracks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export type UseTracksOptions = {
2525

2626
/** @public */
2727
export type UseTracksHookReturnType<T> = T extends Track.Source[]
28-
? Array<TrackReference<T[0]>>
28+
? TrackReference[]
2929
: T extends TrackSourceWithOptions[]
30-
? Array<TrackReferenceOrPlaceholder<T[0]['source']>>
30+
? TrackReferenceOrPlaceholder[]
3131
: never;
3232

3333
/**

0 commit comments

Comments
 (0)