Skip to content

Commit

Permalink
Update global audio volume exclusively through useReplContext effect
Browse files Browse the repository at this point in the history
This ensures the audio volume is synced across tabs, since changing the volume in one tab would retrigger the effect in the other tabs.

It also means we can remove the explicit call to `setGlobalAudioVolume()` from <SettingsTab>
  • Loading branch information
netux committed Sep 3, 2024
1 parent ccb1256 commit 753b08e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
5 changes: 1 addition & 4 deletions website/src/repl/components/panel/SettingsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ export function SettingsTab({ started }) {
)}
<NumberSlider
value={audioVolume}
onChange={(audioVolume) => {
settingsMap.setKey('audioVolume', audioVolume);
setGlobalAudioVolume(audioVolume);
}}
onChange={(audioVolume) => settingsMap.setKey('audioVolume', audioVolume)}
min={0}
max={100}
step={0.1}
Expand Down
11 changes: 5 additions & 6 deletions website/src/repl/useReplContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ export function useReplContext() {
editorRef.current?.updateSettings(editorSettings);
}, [_settings]);

// on first load...
// on first load, set stored audio device if possible
useEffect(() => {
const { audioDeviceName, audioVolume } = _settings;
const { audioDeviceName } = _settings;

// set stored audio device if possible
if (audioDeviceName !== defaultAudioDeviceName) {
getAudioDevices().then((devices) => {
const deviceID = devices.get(audioDeviceName);
Expand All @@ -173,11 +172,11 @@ export function useReplContext() {
setAudioDevice(deviceID);
});
}

// set stored audio volume
setGlobalAudioVolume(audioVolume);
}, []);

// set stored audio volume
useEffect(() => setGlobalAudioVolume(_settings.audioVolume), [_settings.audioVolume]);

//
// UI Actions
//
Expand Down

0 comments on commit 753b08e

Please sign in to comment.