Files
Psychotoxical-psysonic/src/features/playback/store/queuePlaybackIdle.ts
T
Psychotoxical cb1a110afb refactor(playback): move the audio engine into features/playback
Relocate the playback/queue/transport/audio-output engine out of the type-first
store/ + utils/playback/ + utils/audio/ dirs into a cohesive src/features/playback/,
structure-preserving:
  store/<x>                    -> features/playback/store/<x>
  store/audioListenerSetup/<x> -> features/playback/store/audioListenerSetup/<x>
  utils/playback/<x>           -> features/playback/utils/playback/<x>
  utils/audio/<x>              -> features/playback/utils/audio/<x>

184 files moved (107 source + 77 tests), 365 consumers rewritten. Pure move — no
behavior change, no state-split (the playerStore state-split stays a separate M5
question). Enabled by this session's decouple seams (artist/offline/orbit/auth →
core registries), so the engine carries no inbound core->feature inversion: store/
now holds only the 50 cross-cutting global stores (auth family, the seams, library
index, UI/settings stores).

KEPT OUT of the move (would re-create global->engine edges): the 3 pure config
helpers utils/audio/{loudnessPreAnalysisSlider,hiResCrossfadeResample} +
utils/playback/autodjOverlapCap (authStore + settings UI read them — they stay in
utils/). Ambiguous view-state stores (eqStore, queueToolbarStore,
playerBarLayoutStore) stay global (no engine imports).

Consumers use DEEP paths (@/features/playback/...), no barrel — matches the lib/
approach and avoids barrel-mock-collapse across the 140 usePlayerStore consumers.
Two tolerated type-only core->feature edges remain (localPlaybackStore->QueueItemRef,
localPlaybackMigration->HotCacheEntry, both erased).

tsc 0, lint 0, full suite 319/2353 green, iron-rule clean (no runtime store->feature
import). Behavior-touching only via the prerequisite bridge seam (already QA-flagged);
the move itself is pure.
2026-06-30 15:00:20 +02:00

101 lines
2.7 KiB
TypeScript

/** Timestamps and flags for idle auto-pull guards (play queue sync). */
let playbackIdleSinceMs = 0;
let lastQueueMutationAt = 0;
/** When true, idle auto-pull is disabled until manual pull re-enables it. */
let idleQueuePullSuspended = false;
/** Set when repeat-off playback reaches the queue tail — blocks idle pull until play resumes. */
let queueNaturallyEnded = false;
/** Bumped on each local queue mutation; stale in-flight idle pulls must not apply. */
let idlePullGeneration = 0;
const idlePullSuspensionListeners = new Set<() => void>();
function emitIdlePullSuspensionChange(): void {
for (const listener of idlePullSuspensionListeners) {
listener();
}
}
export function subscribeIdleQueuePullSuspended(listener: () => void): () => void {
idlePullSuspensionListeners.add(listener);
return () => idlePullSuspensionListeners.delete(listener);
}
export function getIdleQueuePullSuspendedSnapshot(): boolean {
return idleQueuePullSuspended;
}
export function markPlaybackIdle(): void {
if (playbackIdleSinceMs === 0) playbackIdleSinceMs = Date.now();
}
export function markPlaybackActive(): void {
playbackIdleSinceMs = 0;
clearQueueNaturallyEnded();
}
export function markQueueNaturallyEnded(): void {
queueNaturallyEnded = true;
}
export function clearQueueNaturallyEnded(): void {
queueNaturallyEnded = false;
}
export function isQueueNaturallyEnded(): boolean {
return queueNaturallyEnded;
}
export function getPlaybackIdleSinceMs(): number {
return playbackIdleSinceMs;
}
export function isPlaybackIdleLongEnough(thresholdMs: number): boolean {
return playbackIdleSinceMs > 0 && Date.now() - playbackIdleSinceMs >= thresholdMs;
}
export function suspendIdleQueuePull(): void {
if (idleQueuePullSuspended) return;
idleQueuePullSuspended = true;
emitIdlePullSuspensionChange();
}
export function resumeIdleQueuePull(): void {
if (!idleQueuePullSuspended) return;
idleQueuePullSuspended = false;
emitIdlePullSuspensionChange();
}
export function isIdleQueuePullSuspended(): boolean {
return idleQueuePullSuspended;
}
export function getIdlePullGeneration(): number {
return idlePullGeneration;
}
export function touchQueueMutationClock(): void {
lastQueueMutationAt = Date.now();
clearQueueNaturallyEnded();
suspendIdleQueuePull();
idlePullGeneration += 1;
}
export function getLastQueueMutationAt(): number {
return lastQueueMutationAt;
}
export function hasRecentQueueMutation(withinMs: number): boolean {
return lastQueueMutationAt > 0 && Date.now() - lastQueueMutationAt < withinMs;
}
/** Test-only reset. */
export function _resetQueuePlaybackIdleForTest(): void {
playbackIdleSinceMs = 0;
lastQueueMutationAt = 0;
idleQueuePullSuspended = false;
queueNaturallyEnded = false;
idlePullGeneration = 0;
}