From d901045e55a29454cf69ed3ea7be734fff94ad2f Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 22 Oct 2024 17:23:40 -0400 Subject: [PATCH] Address review comments --- src/Slider.tsx | 6 ++++++ src/state/MediaViewModel.ts | 38 +++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/Slider.tsx b/src/Slider.tsx index 750836f6..aa9309e7 100644 --- a/src/Slider.tsx +++ b/src/Slider.tsx @@ -16,6 +16,12 @@ interface Props { label: string; value: number; onValueChange: (value: number) => void; + /** + * Event handler called when the value changes at the end of an interaction. + * Useful when you only need to capture a final value to update a backend + * service, or when you want to remember the last value that the user + * "committed" to. + */ onValueCommit?: (value: number) => void; min: number; max: number; diff --git a/src/state/MediaViewModel.ts b/src/state/MediaViewModel.ts index 60e2a061..26894f9e 100644 --- a/src/state/MediaViewModel.ts +++ b/src/state/MediaViewModel.ts @@ -248,22 +248,28 @@ export class RemoteUserMediaViewModel extends BaseUserMediaViewModel { this.localVolumeAdjustment, this.localVolumeCommit.pipe(map(() => "commit" as const)), ).pipe( - accumulate( - { muted: false, volume: 1, committedVolume: 1 }, - (state, event) => - event === "toggle mute" - ? { ...state, muted: !state.muted } - : event === "commit" - ? { ...state, committedVolume: state.volume } - : // Volume adjustment - event === 0 - ? // Dragging the slider to zero should have the same effect as - // muting: reset the volume to the committed volume, as if it were - // never dragged - { ...state, muted: true, volume: state.committedVolume } - : { ...state, muted: false, volume: event }, - ), - map(({ muted, volume }) => (muted ? 0 : volume)), + accumulate({ volume: 1, committedVolume: 1 }, (state, event) => { + switch (event) { + case "toggle mute": + return { + ...state, + volume: state.volume === 0 ? state.committedVolume : 0, + }; + case "commit": + // Dragging the slider to zero should have the same effect as + // muting: keep the original committed volume, as if it were never + // dragged + return { + ...state, + committedVolume: + state.volume === 0 ? state.committedVolume : state.volume, + }; + default: + // Volume adjustment + return { ...state, volume: event }; + } + }), + map(({ volume }) => volume), this.scope.state(), );