Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race conditions in usePersistedDevicePreferences #1575

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
37 changes: 29 additions & 8 deletions packages/react-sdk/src/hooks/usePersistedDevicePreferences.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { type MutableRefObject, useEffect, useRef } from 'react';
import { CallingState } from '@stream-io/video-client';
import { useCall, useCallStateHooks } from '@stream-io/video-react-bindings';

Expand All @@ -18,14 +18,18 @@ const defaultDevice = 'default';
*
* @param key the key to use for local storage.
*/
const usePersistDevicePreferences = (key: string) => {
const usePersistDevicePreferences = (
key: string,
shouldPersistRef: MutableRefObject<boolean>,
) => {
const { useMicrophoneState, useCameraState, useSpeakerState } =
useCallStateHooks();
const call = useCall();
const mic = useMicrophoneState();
const camera = useCameraState();
const speaker = useSpeakerState();
useEffect(() => {
if (!shouldPersistRef.current) return;
if (!call) return;
if (call.state.callingState === CallingState.LEFT) return;
try {
Expand Down Expand Up @@ -55,6 +59,7 @@ const usePersistDevicePreferences = (key: string) => {
mic.isMute,
mic.selectedDevice,
speaker.selectedDevice,
shouldPersistRef,
]);
};

Expand All @@ -63,15 +68,21 @@ const usePersistDevicePreferences = (key: string) => {
*
* @param key the key to use for local storage.
*/
const useApplyDevicePreferences = (key: string) => {
const useApplyDevicePreferences = (key: string, onApplied: () => void) => {
const call = useCall();
const onAppliedRef = useRef(onApplied);
onAppliedRef.current = onApplied;
useEffect(() => {
if (!call) return;
if (call.state.callingState === CallingState.LEFT) return;

let cancel = false;

const apply = async () => {
const initMic = async (setting: LocalDevicePreference) => {
if (cancel) return;
await call.microphone.select(parseDeviceId(setting.selectedDeviceId));
if (cancel) return;
if (setting.muted) {
await call.microphone.disable();
} else {
Expand All @@ -80,7 +91,9 @@ const useApplyDevicePreferences = (key: string) => {
};

const initCamera = async (setting: LocalDevicePreference) => {
if (cancel) return;
await call.camera.select(parseDeviceId(setting.selectedDeviceId));
if (cancel) return;
if (setting.muted) {
await call.camera.disable();
} else {
Expand All @@ -89,6 +102,7 @@ const useApplyDevicePreferences = (key: string) => {
};

const initSpeaker = (setting: LocalDevicePreference) => {
if (cancel) return;
call.speaker.select(parseDeviceId(setting.selectedDeviceId) ?? '');
};

Expand All @@ -107,9 +121,15 @@ const useApplyDevicePreferences = (key: string) => {
}
};

apply().catch((err) => {
console.warn('Failed to apply device preferences', err);
});
apply()
.then(() => onAppliedRef.current())
.catch((err) => {
console.warn('Failed to apply device preferences', err);
});

return () => {
cancel = true;
};
}, [call, key]);
};

Expand All @@ -121,8 +141,9 @@ const useApplyDevicePreferences = (key: string) => {
export const usePersistedDevicePreferences = (
key: string = '@stream-io/device-preferences',
) => {
useApplyDevicePreferences(key);
usePersistDevicePreferences(key);
const shouldPersistRef = useRef(false);
useApplyDevicePreferences(key, () => (shouldPersistRef.current = true));
usePersistDevicePreferences(key, shouldPersistRef);
};

const parseDeviceId = (deviceId: string) =>
Expand Down
Loading