From 5262af700066b5fd314e2a0833f628d70c0677ec Mon Sep 17 00:00:00 2001 From: Timo <16718859+toger5@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:51:37 +0200 Subject: [PATCH] Fix sync loop by adding a 20ms break for the next mute sync (#1742) * fix sync loop by adding a 20ms break for the next mute sync --------- Signed-off-by: Timo K --- src/livekit/useLiveKit.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/livekit/useLiveKit.ts b/src/livekit/useLiveKit.ts index c8a5e401..a4330c79 100644 --- a/src/livekit/useLiveKit.ts +++ b/src/livekit/useLiveKit.ts @@ -166,6 +166,12 @@ export function useLiveKit( logger.error("Failed to sync audio mute state with LiveKit", e); } audioMuteUpdating.current = false; + // await participant.setMicrophoneEnabled can return immediately in some instances, + // so that participant.isMicrophoneEnabled !== buttonEnabled.current.audio still holds true. + // This happens if the device is still in a pending state + // "sleeping" here makes sure we let react do its thing so that participant.isMicrophoneEnabled is updated, + // so we do not end up in a recursion loop. + await new Promise((r) => setTimeout(r, 20)); // Run the check again after the change is done. Because the user // can update the state (presses mute button) while the device is enabling // itself we need might need to update the mute state right away. @@ -187,6 +193,8 @@ export function useLiveKit( } videoMuteUpdating.current = false; // see above + await new Promise((r) => setTimeout(r, 20)); + // see above syncMuteStateVideo(); } };