Skip to main content

November 27, 2026

React stage reducer beats voice-floor latches

Voice-guided practice flickered between guide and clip when ten booleans watched isSpeaking. I replaced them with narrating, practising, and asking modes.

Sander Korf4 min read
reacttypescriptaudio

Someone rehearsing out loud in a React and TypeScript web app sees one big panel in the middle of the screen. A speaking guide (video avatar, or a voice wheel in voice-only mode) alternates with a looping instructional clip. Under that stage sit clickable answer cards and a twenty-second practice timer that only runs while the clip is showing. During a real live spoken session the stage pulsed wrong: after a rating tap it jumped guide, old clip, guide, then the next clip. When a fresh round began, the next clip appeared while the guide was still introducing it.

The voice floor is whether the guide's audio or text-to-speech output is playing right now: while that output runs, isSpeaking is true; during breath gaps between sentences it drops false. It is not the user's microphone channel. A latch is a boolean that remembers "we already handed the stage to the clip" or "answers stay locked until the guide finishes." I had ten of those latches, each watching the voice floor with its own timing. Another layer merged the booleans afterward. One latch flashed the previous clip on a brief pause between sentences. Another opened the new clip because local lines looked finished even though the server reply was still talking.

A later phase of the same app already followed one rule: show the clip only once this visit's lines are said through and the guide is not speaking. That path never flickered. The broken phase was still splitting the same screen across ten independent latches.

Three modes instead of ten booleans

I deleted the scattered latches and shipped one pure stageVisitReducer with three explicit modes. In narrating the guide owns the stage, captions show, the timer stays stopped, and answer cards hide or lock. In practising the clip loops, cards open, and the twenty-second timer runs. In asking the guide keeps the stage while rating or pick cards stay clickable.

Raw isSpeaking is not an input. The only exit from narrating is a latched saidThrough event, counted per stretch of speech (first the reply stretch, then the step's own lines). Typed or muted sessions use a read-time fallback to fire the same event. Pause, Stop, or a connection drop sets held, freezes transitions, and restarts the stretch counter so Resume never treats half a sentence as complete.

Stage panel, answer cards, and timer all read mode from one Context. A pure stageVisitSync helper diffs inputs and dispatches only actions the reducer accepts, so one render settles.

Simulation

Stage panel: latches vs reducer

Fake voice-guided practice session. Compare stage ownership, answer cards, and the 20-second timer when latches disagree vs one reducer mode.

Central stage

Speaking guide

Guide asks how the exercise felt

Answer rating (150ms voice gap)

Stage ownership (so far)

guide

Scattered wiring replays guide → clip 1 → guide → clip 2 on a short voice gap.

Wiring
Scenario

Flip between scattered latches and the three-mode reducer, pick Answer rating or New round intro, and run the scenario to watch the ownership strip under the stage.

type StageMode = 'narrating' | 'practising' | 'asking'
 
type StageState = {
  visit: string
  mode: StageMode
  hasClip: boolean
  awaited: boolean
  held: boolean
}
 
type StageAction =
  | { type: 'visit'; key: string; hasClip: boolean }
  | { type: 'saidThrough'; visit: string }
  | { type: 'replyAwaited'; awaited: boolean }
  | { type: 'held'; held: boolean }
 
function stageReducer(state: StageState, action: StageAction): StageState {
  switch (action.type) {
    case 'visit':
      return action.key === state.visit ? state : enterVisit(state, action)
    case 'saidThrough':
      if (state.mode !== 'narrating' || state.visit !== action.visit) {
        return state
      }
      return { ...state, mode: state.hasClip ? 'practising' : 'asking' }
    case 'replyAwaited':
      return state.awaited === action.awaited ? state : { ...state, awaited: action.awaited }
    case 'held':
      return state.held === action.held ? state : { ...state, held: action.held }
    default:
      return state
  }
}

enterVisit resets mode to narrating for a new key. stageVisitSync only dispatches saidThrough when the visit matches, mode is still narrating, no reply is awaited, and nothing is held.

What I traded away

When a server reply carries part of the next step's spoken lines and the client speaks the rest, the clip starts about one sentence later. The machine waits for both stretches to finish. That delay is intentional. Twenty-four reducer and sync unit tests plus eleven input tests landed with the change; they rode along with thousands of existing web unit tests.

I also wrote a Playwright mechanics test that samples data-shown on the stage every hundred milliseconds and collapses runs. It typechecked in the same change set. I did not run it in CI against the old latch code, so I cannot claim it would have caught the live flicker before the refactor.

If your UI couples stage, forms, and timers to live audio edges, try naming modes first and drive them from completion events, not from isSpeaking alone. Latches feel local until a gap between sentences proves they disagree.


Happy coding!

Sander