Files
psysonic/src/utils/switchActiveServer.ts
T
Frank Stellmacher 4d564e5016 refactor(auth): E.43 — extract authStore types + defaults + helpers (#608)
First slice of the authStore split. Pure code-move: no behaviour change.

- `authStoreTypes.ts` — `ServerProfile`, `AuthState`, plus all union
  types (`SeekbarStyle`, `LoggingMode`, `NormalizationEngine`,
  `DiscordCoverSource`, `LoudnessLufsPreset`, `LyricsSourceId`,
  `LyricsSourceConfig`, `TrackPreviewLocation`, `TrackPreviewLocations`).
- `authStoreDefaults.ts` — `LOUDNESS_LUFS_PRESETS`,
  `DEFAULT_LOUDNESS_PRE_ANALYSIS_ATTENUATION_DB`,
  `TRACK_PREVIEW_LOCATIONS`, `DEFAULT_TRACK_PREVIEW_LOCATIONS`,
  `DEFAULT_LYRICS_SOURCES`, `MIX_MIN_RATING_FILTER_MAX_STARS`,
  `RANDOM_MIX_SIZE_OPTIONS`.
- `authStoreHelpers.ts` — `generateId`,
  `sanitizeLoudnessLufsPreset`, `sanitizeLoudnessPreAnalysisFromStorage`,
  `clampMixFilterMinStars`, `clampRandomMixSize`,
  `clampSkipStarThreshold`, `skipStarCountStorageKey`,
  `sanitizeSkipStarCounts`.

12 external call sites migrated to direct imports from the new
modules (no re-export shims left in authStore.ts — applies the
[feedback_prevent_god_modules] rule 5: avoid re-export debt).

authStore.ts: 889 → 518 LOC (−371).
2026-05-12 23:22:28 +02:00

44 lines
1.8 KiB
TypeScript

import type { ServerProfile } from '../store/authStoreTypes';
import { pingWithCredentials, scheduleInstantMixProbeForServer } from '../api/subsonic';
import { useAuthStore } from '../store/authStore';
import { useOrbitStore } from '../store/orbitStore';
import { endOrbitSession, leaveOrbitSession } from './orbit';
export async function switchActiveServer(server: ServerProfile): Promise<boolean> {
try {
const ping = await pingWithCredentials(server.url, server.username, server.password);
if (!ping.ok) return false;
// Tear down any active Orbit session before we actually switch. The
// session's playlists live on the *old* server — once we flip the
// active server, every API call from the orbit hooks would hit the
// wrong backend, heartbeats would silently fail, and the next
// app-start cleanup would prune the still-live session as stale.
// Capped at 1.5 s so a slow network doesn't freeze the UI.
const role = useOrbitStore.getState().role;
if (role === 'host' || role === 'guest') {
const teardown = role === 'host' ? endOrbitSession() : leaveOrbitSession();
await Promise.race([
teardown.catch(() => {}),
new Promise<void>(r => setTimeout(r, 1500)),
]);
// Ensure local store is idle even if the remote call timed out.
useOrbitStore.getState().reset();
}
const identity = {
type: ping.type,
serverVersion: ping.serverVersion,
openSubsonic: ping.openSubsonic,
};
const auth = useAuthStore.getState();
auth.setSubsonicServerIdentity(server.id, identity);
scheduleInstantMixProbeForServer(server.id, server.url, server.username, server.password, identity);
auth.setActiveServer(server.id);
auth.setLoggedIn(true);
return true;
} catch {
return false;
}
}