Files
Psychotoxical-psysonic/src/store/playbackThrottles.ts
T
Frank Stellmacher c10eabe114 refactor(player): E.21 — extract playback-progress throttle timestamps (#585)
Three time-based throttles used by the audio-progress handler move into
`src/store/playbackThrottles.ts`:

  - **Live progress emit** (1.5 s / 0.9 s position delta) — feeds the
    high-frequency pub/sub
  - **Store progress commit** (20 s / 5 s position delta) — writes the
    Zustand store
  - **Normalization UI update** (120 ms) — live dB readout throttle

Module owns three private mutables, exports five constants (the four
window/delta gates + NORMALIZATION_UI_THROTTLE_MS) and six accessors
(get / mark for each), plus `resetProgressEmitThrottles` for the
track-boundary reset path that handleAudioPlaying calls.

playerStore's six direct-access sites + two reset writes collapse to
imports. Behaviour preserved verbatim.

13 focused tests pin each throttle's get/mark cycle, the partial reset
(progress only, not normalization), and constant values.

playerStore +6 LOC net (more import surface than freed mutables) —
shrinkage will come back on the next bigger encapsulation; this PR's
win is logical separation + testability rather than line count.
2026-05-12 16:45:57 +02:00

65 lines
2.3 KiB
TypeScript

/**
* Three time-based throttles used by the audio-progress handler so it
* doesn't saturate the WebView2 renderer with redundant emits / setState
* commits on a noisy `audio:progress` stream:
*
* - **Live progress emit** — feeds the high-frequency `playbackProgress`
* pub/sub. Gate: 1.5 s elapsed OR ≥0.9 s position delta.
* - **Store progress commit** — writes the Zustand player store. Gate:
* 20 s elapsed OR ≥5 s position delta. Heavier than the live emit
* because subscribers to the store re-render.
* - **Normalization UI update** — 120 ms throttle on the live dB readout
* written to the store during a track.
*
* The progress emit + commit pair share `resetProgressEmitThrottles` so
* the runtime can force a fresh emit/commit immediately after a track
* boundary (`handleAudioPlaying`).
*/
export const LIVE_PROGRESS_EMIT_MIN_MS = 1500;
export const LIVE_PROGRESS_EMIT_MIN_DELTA_SEC = 0.9;
export const STORE_PROGRESS_COMMIT_MIN_MS = 20_000;
export const STORE_PROGRESS_COMMIT_MIN_DELTA_SEC = 5.0;
export const NORMALIZATION_UI_THROTTLE_MS = 120;
let lastLiveProgressEmitAt = 0;
let lastStoreProgressCommitAt = 0;
let lastNormalizationUiUpdateAtMs = 0;
export function getLastLiveProgressEmitAt(): number {
return lastLiveProgressEmitAt;
}
export function markLiveProgressEmit(nowMs: number): void {
lastLiveProgressEmitAt = nowMs;
}
export function getLastStoreProgressCommitAt(): number {
return lastStoreProgressCommitAt;
}
export function markStoreProgressCommit(nowMs: number): void {
lastStoreProgressCommitAt = nowMs;
}
export function getLastNormalizationUiUpdateAtMs(): number {
return lastNormalizationUiUpdateAtMs;
}
export function markNormalizationUiUpdate(nowMs: number): void {
lastNormalizationUiUpdateAtMs = nowMs;
}
/** Reset the two progress throttles — used by `handleAudioPlaying` so the first emit + commit after a track boundary go through immediately. */
export function resetProgressEmitThrottles(): void {
lastLiveProgressEmitAt = 0;
lastStoreProgressCommitAt = 0;
}
/** Test-only: wipe all three timestamps so each spec starts fresh. */
export function _resetPlaybackThrottlesForTest(): void {
lastLiveProgressEmitAt = 0;
lastStoreProgressCommitAt = 0;
lastNormalizationUiUpdateAtMs = 0;
}