Refactor media devices to live outside React as Observables (#3334)
* Refactor media devices to live outside React as Observables This moves the media devices state out of React to further our transition to a MVVM architecture in which we can more easily model and store complex application state. I have created an AppViewModel to act as the overarching state holder for any future non-React state we end up creating, and the MediaDevices reside within this. We should move more application logic (including the CallViewModel itself) there in the future. * Address review feedback * Fixes from ios debugging session: (#3342) - dont use preferred vs selected concept in controlled media. Its not needed since we dont use the id for actual browser media devices (the id's are not even actual browser media devices) - add more logging - add more conditions to not accidently set a deviceId that is not a browser deviceId but one provided via controlled. --------- Co-authored-by: Timo <16718859+toger5@users.noreply.github.com>
This commit is contained in:
@@ -14,11 +14,14 @@ import {
|
||||
} from "react";
|
||||
import { type IWidgetApiRequest } from "matrix-widget-api";
|
||||
import { logger } from "matrix-js-sdk/lib/logger";
|
||||
import { useObservableEagerState } from "observable-hooks";
|
||||
|
||||
import {
|
||||
type MediaDeviceHandle,
|
||||
useMediaDevices,
|
||||
} from "../livekit/MediaDevicesContext";
|
||||
type DeviceLabel,
|
||||
type SelectedDevice,
|
||||
type MediaDevice,
|
||||
} from "../state/MediaDevices";
|
||||
import { useMediaDevices } from "../MediaDevicesContext";
|
||||
import { useReactiveState } from "../useReactiveState";
|
||||
import { ElementWidgetActions, widget } from "../widget";
|
||||
import { Config } from "../config/Config";
|
||||
@@ -53,24 +56,24 @@ export interface MuteStates {
|
||||
}
|
||||
|
||||
function useMuteState(
|
||||
device: MediaDeviceHandle,
|
||||
device: MediaDevice<DeviceLabel, SelectedDevice>,
|
||||
enabledByDefault: () => boolean,
|
||||
): MuteState {
|
||||
const available = useObservableEagerState(device.available$);
|
||||
const [enabled, setEnabled] = useReactiveState<boolean | undefined>(
|
||||
// Determine the default value once devices are actually connected
|
||||
(prev) =>
|
||||
prev ?? (device.available.size > 0 ? enabledByDefault() : undefined),
|
||||
[device.available.size],
|
||||
(prev) => prev ?? (available.size > 0 ? enabledByDefault() : undefined),
|
||||
[available.size],
|
||||
);
|
||||
return useMemo(
|
||||
() =>
|
||||
device.available.size === 0
|
||||
available.size === 0
|
||||
? deviceUnavailable
|
||||
: {
|
||||
enabled: enabled ?? false,
|
||||
setEnabled: setEnabled as Dispatch<SetStateAction<boolean>>,
|
||||
},
|
||||
[device.available.size, enabled, setEnabled],
|
||||
[available.size, enabled, setEnabled],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user