refactor(player): E.13 — extract loudness-backfill retry state (#576)

The two parallel maps that bound the per-track loudness backfill
retries (`analysisBackfillInFlightByTrackId`,
`analysisBackfillAttemptsByTrackId`) plus the `MAX_BACKFILL_ATTEMPTS_PER_TRACK`
constant and the `resetLoudnessBackfillStateForTrackId` reseed helper
move into `src/store/loudnessBackfillState.ts`. The new module exposes
a thin API:

  - `isBackfillInFlight` / `getBackfillAttempts` (reads)
  - `markBackfillInFlight(trackId, nextAttempt)` (atomic flag + counter)
  - `clearBackfillInFlight` (after promise settles)
  - `resetBackfillAttempts` (after refresh-hit)
  - `resetLoudnessBackfillStateForTrackId` (full reset across both id forms)

playerStore's five direct-access sites inside `refreshLoudnessForTrack`
become API calls; the mutables are no longer reachable from outside the
module. 13 focused tests pin atomicity, independence between tracks,
the partial-clear shapes (flag-only / counter-only), and the two-form
reseed expansion.

playerStore 3263 → 3261 LOC (small line delta because the inflight
flag + counter setup collapses to one call but the reset helper is no
longer inline).
This commit is contained in:
Frank Stellmacher
2026-05-12 14:29:33 +02:00
committed by GitHub
parent 85df3e42c1
commit c88649836e
3 changed files with 161 additions and 16 deletions
+91
View File
@@ -0,0 +1,91 @@
/**
* Backfill state: two parallel maps that retry the per-track loudness
* analysis a bounded number of times. The interesting behaviours are the
* `markBackfillInFlight` atomicity (both flag + counter bump in one call)
* and the reseed reset that expands across the `stream:` / bare id forms
* via `loudnessCacheStateKeysForTrackId` (re-used from loudnessGainCache).
*/
import { afterEach, describe, expect, it } from 'vitest';
import {
MAX_BACKFILL_ATTEMPTS_PER_TRACK,
_resetBackfillStateForTest,
clearBackfillInFlight,
getBackfillAttempts,
isBackfillInFlight,
markBackfillInFlight,
resetBackfillAttempts,
resetLoudnessBackfillStateForTrackId,
} from './loudnessBackfillState';
afterEach(() => {
_resetBackfillStateForTest();
});
describe('initial state', () => {
it('reports no inflight + 0 attempts for unknown tracks', () => {
expect(isBackfillInFlight('t1')).toBe(false);
expect(getBackfillAttempts('t1')).toBe(0);
});
});
describe('markBackfillInFlight', () => {
it('atomically sets inflight flag and counter', () => {
markBackfillInFlight('t1', 1);
expect(isBackfillInFlight('t1')).toBe(true);
expect(getBackfillAttempts('t1')).toBe(1);
});
it('keeps tracks independent', () => {
markBackfillInFlight('a', 1);
markBackfillInFlight('b', 2);
expect(getBackfillAttempts('a')).toBe(1);
expect(getBackfillAttempts('b')).toBe(2);
clearBackfillInFlight('a');
expect(isBackfillInFlight('a')).toBe(false);
expect(isBackfillInFlight('b')).toBe(true);
});
});
describe('clearBackfillInFlight', () => {
it('clears the flag without touching the counter', () => {
markBackfillInFlight('t1', 1);
clearBackfillInFlight('t1');
expect(isBackfillInFlight('t1')).toBe(false);
expect(getBackfillAttempts('t1')).toBe(1); // counter preserved
});
});
describe('resetBackfillAttempts', () => {
it('zeros the counter without touching the inflight flag', () => {
markBackfillInFlight('t1', 2);
resetBackfillAttempts('t1');
expect(getBackfillAttempts('t1')).toBe(0);
expect(isBackfillInFlight('t1')).toBe(true);
});
});
describe('MAX_BACKFILL_ATTEMPTS_PER_TRACK', () => {
it('is the hard-coded threshold the runtime uses', () => {
expect(MAX_BACKFILL_ATTEMPTS_PER_TRACK).toBe(2);
});
});
describe('resetLoudnessBackfillStateForTrackId', () => {
it('clears both maps for both id forms (bare + stream:)', () => {
markBackfillInFlight('t1', 1);
markBackfillInFlight('stream:t1', 2);
resetLoudnessBackfillStateForTrackId('t1');
expect(isBackfillInFlight('t1')).toBe(false);
expect(isBackfillInFlight('stream:t1')).toBe(false);
expect(getBackfillAttempts('t1')).toBe(0);
expect(getBackfillAttempts('stream:t1')).toBe(0);
});
it('also works when invoked with the stream-prefixed form', () => {
markBackfillInFlight('t1', 1);
markBackfillInFlight('stream:t1', 2);
resetLoudnessBackfillStateForTrackId('stream:t1');
expect(getBackfillAttempts('t1')).toBe(0);
expect(getBackfillAttempts('stream:t1')).toBe(0);
});
});