Skip to content

Commit

Permalink
Forward track toggle errors (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored May 17, 2024
1 parent fb24984 commit b913610
Showing 6 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/nine-peas-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/components-core": patch
"@livekit/components-react": patch
---

Forward track toggle errors
2 changes: 1 addition & 1 deletion packages/core/etc/components-core.api.md
Original file line number Diff line number Diff line change
@@ -490,7 +490,7 @@ export function setupManualToggle(): {
};

// @public (undocumented)
export function setupMediaToggle<T extends ToggleSource>(source: T, room: Room, options?: CaptureOptionsBySource<T>, publishOptions?: TrackPublishOptions): MediaToggleType<T>;
export function setupMediaToggle<T extends ToggleSource>(source: T, room: Room, options?: CaptureOptionsBySource<T>, publishOptions?: TrackPublishOptions, onError?: (error: Error) => void): MediaToggleType<T>;

// Warning: (ae-incompatible-release-tags) The symbol "setupMediaTrack" is marked as @public, but its signature references "TrackIdentifier" which is marked as @internal
//
7 changes: 7 additions & 0 deletions packages/core/src/components/mediaToggle.ts
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ export function setupMediaToggle<T extends ToggleSource>(
room: Room,
options?: CaptureOptionsBySource<T>,
publishOptions?: TrackPublishOptions,
onError?: (error: Error) => void,
): MediaToggleType<T> {
const { localParticipant } = room;

@@ -96,6 +97,12 @@ export function setupMediaToggle<T extends ToggleSource>(
default:
break;
}
} catch (e) {
if (onError && e instanceof Error) {
onError?.(e);
} else {
throw e;
}
} finally {
pendingSubject.next(false);
// trigger observable update
4 changes: 3 additions & 1 deletion packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
@@ -642,6 +642,8 @@ export interface TrackToggleProps<T extends ToggleSource> extends Omit<React_2.B
initialState?: boolean;
onChange?: (enabled: boolean, isUserInitiated: boolean) => void;
// (undocumented)
onDeviceError?: (error: Error) => void;
// (undocumented)
publishOptions?: TrackPublishOptions;
// (undocumented)
showIcon?: boolean;
@@ -1089,7 +1091,7 @@ export type UseTracksOptions = {
};

// @public
export function useTrackToggle<T extends ToggleSource>({ source, onChange, initialState, captureOptions, publishOptions, ...rest }: UseTrackToggleProps<T>): {
export function useTrackToggle<T extends ToggleSource>({ source, onChange, initialState, captureOptions, publishOptions, onDeviceError, ...rest }: UseTrackToggleProps<T>): {
toggle: (forceState?: boolean | undefined, captureOptions?: CaptureOptionsBySource<T> | undefined) => Promise<void>;
enabled: boolean;
pending: boolean;
1 change: 1 addition & 0 deletions packages/react/src/components/controls/TrackToggle.tsx
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export interface TrackToggleProps<T extends ToggleSource>
onChange?: (enabled: boolean, isUserInitiated: boolean) => void;
captureOptions?: CaptureOptionsBySource<T>;
publishOptions?: TrackPublishOptions;
onDeviceError?: (error: Error) => void;
}

/**
5 changes: 3 additions & 2 deletions packages/react/src/hooks/useTrackToggle.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ export function useTrackToggle<T extends ToggleSource>({
initialState,
captureOptions,
publishOptions,
onDeviceError,
...rest
}: UseTrackToggleProps<T>) {
const room = useMaybeRoomContext();
@@ -37,9 +38,9 @@ export function useTrackToggle<T extends ToggleSource>({
const { toggle, className, pendingObserver, enabledObserver } = React.useMemo(
() =>
room
? setupMediaToggle<T>(source, room, captureOptions, publishOptions)
? setupMediaToggle<T>(source, room, captureOptions, publishOptions, onDeviceError)
: setupManualToggle(),
[room, source, JSON.stringify(captureOptions), JSON.stringify(publishOptions)],
[room, source, JSON.stringify(captureOptions), publishOptions],
);

const pending = useObservableState(pendingObserver, false);

0 comments on commit b913610

Please sign in to comment.