Files
Psychotoxical-psysonic/src/store/playbackUrlRouting.ts
T
Frank Stellmacher b68bddd034 refactor(player): E.5 — extract playback-URL routing into module (#568)
Three file-private helpers + their shared module-scoped track id move
into src/store/playbackUrlRouting.ts: recordEnginePlayUrl,
playbackSourceHintForResolvedUrl, shouldRebindPlaybackToHotCache. The
`lastOpenedWithHttpTrackId` mutable goes with them — only those three
read it. No external callers, no re-exports needed.

Adds 12 focused tests covering the source-kind classifier
(stream / hot / offline), the rebind decision across stream:-prefix
forms, the empty-serverId / un-recorded edge cases, and the
test-only reset helper.

playerStore 3490 → 3470 LOC.
2026-05-12 12:07:30 +02:00

40 lines
1.8 KiB
TypeScript

import { resolvePlaybackUrl, type PlaybackSourceKind } from '../utils/resolvePlaybackUrl';
import { sameQueueTrackId } from '../utils/queueIdentity';
import { useOfflineStore } from './offlineStore';
/**
* Helpers that classify the URL `audio_play` was last invoked with, so the
* runtime can rebind playback onto a freshly-promoted hot-cache entry the
* next time the same track plays.
*
* `lastOpenedWithHttpTrackId` remembers the most recent track id we asked
* the engine to fetch over HTTP (anything that isn't the `psysonic-local://`
* scheme). When the hot-cache later promotes that track to a local URL we
* compare against this id to decide whether a transparent rebind is worth
* triggering.
*/
let lastOpenedWithHttpTrackId: string | null = null;
export function recordEnginePlayUrl(trackId: string, url: string): void {
lastOpenedWithHttpTrackId = url.startsWith('psysonic-local://') ? null : trackId;
}
/** Matches `playTrack` / PlayerBar: stream vs hot-cache vs offline file from resolved `audio_play` URL. */
export function playbackSourceHintForResolvedUrl(trackId: string, serverId: string, url: string): PlaybackSourceKind {
if (!url.startsWith('psysonic-local://')) return 'stream';
return useOfflineStore.getState().getLocalUrl(trackId, serverId) ? 'offline' : 'hot';
}
export function shouldRebindPlaybackToHotCache(trackId: string, serverId: string): boolean {
if (!serverId) return false;
if (!lastOpenedWithHttpTrackId || !sameQueueTrackId(lastOpenedWithHttpTrackId, trackId)) {
return false;
}
return resolvePlaybackUrl(trackId, serverId).startsWith('psysonic-local://');
}
/** Test-only: clear the module-scoped track id so each test starts clean. */
export function _resetPlaybackUrlRoutingForTest(): void {
lastOpenedWithHttpTrackId = null;
}