Skip to content

Commit

Permalink
Fix local storage of user choices (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Aug 29, 2024
1 parent 8318a5d commit cb45f42
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/dry-cameras-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/components-core": patch
"@livekit/components-react": patch
---

Fix local storage of user choices
2 changes: 1 addition & 1 deletion packages/core/etc/components-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export const log: loglevel.Logger;
// @public (undocumented)
export type MediaToggleType<T extends ToggleSource> = {
pendingObserver: Observable<boolean>;
toggle: (forceState?: boolean, captureOptions?: CaptureOptionsBySource<T>) => Promise<void>;
toggle: (forceState?: boolean, captureOptions?: CaptureOptionsBySource<T>) => Promise<boolean | undefined>;
className: string;
enabledObserver: Observable<boolean>;
};
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/components/mediaToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export type CaptureOptionsBySource<T extends ToggleSource> = T extends Track.Sou

export type MediaToggleType<T extends ToggleSource> = {
pendingObserver: Observable<boolean>;
toggle: (forceState?: boolean, captureOptions?: CaptureOptionsBySource<T>) => Promise<void>;
toggle: (
forceState?: boolean,
captureOptions?: CaptureOptionsBySource<T>,
) => Promise<boolean | undefined>;
className: string;
enabledObserver: Observable<boolean>;
};
Expand Down Expand Up @@ -79,27 +82,28 @@ export function setupMediaToggle<T extends ToggleSource>(
captureOptions as VideoCaptureOptions,
publishOptions,
);
break;
return localParticipant.isCameraEnabled;
case Track.Source.Microphone:
await localParticipant.setMicrophoneEnabled(
forceState ?? !localParticipant.isMicrophoneEnabled,
captureOptions as AudioCaptureOptions,
publishOptions,
);
break;
return localParticipant.isMicrophoneEnabled;
case Track.Source.ScreenShare:
await localParticipant.setScreenShareEnabled(
forceState ?? !localParticipant.isScreenShareEnabled,
captureOptions as ScreenShareCaptureOptions,
publishOptions,
);
break;
return localParticipant.isScreenShareEnabled;
default:
break;
throw new TypeError('Tried to toggle unsupported source');
}
} catch (e) {
if (onError && e instanceof Error) {
onError?.(e);
return undefined;
} else {
throw e;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ export type UseTracksOptions = {

// @public
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>;
toggle: ((forceState?: boolean | undefined) => Promise<void>) | ((forceState?: boolean | undefined, captureOptions?: CaptureOptionsBySource<T> | undefined) => Promise<boolean | undefined>);
enabled: boolean;
pending: boolean;
track: LocalTrackPublication | undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useTrackToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function useTrackToggle<T extends ToggleSource>({
const clickHandler: React.MouseEventHandler<HTMLButtonElement> = React.useCallback(
(evt) => {
userInteractionRef.current = true;
toggle().finally(() => (userInteractionRef.current = false));
toggle().catch(() => (userInteractionRef.current = false));
rest.onClick?.(evt);
},
[rest, toggle],
Expand Down

0 comments on commit cb45f42

Please sign in to comment.