refactor: clean up code structure and improve error handling in media controllers

This commit is contained in:
MythEclipse
2026-05-16 23:11:46 +07:00
parent 9b211f05cf
commit 7dedac2094
6 changed files with 44 additions and 23 deletions
+12 -12
View File
@@ -52,9 +52,21 @@ export class MediaController {
): Promise<MediaState> {
const mode = options.mode ?? "music";
if (mode === "screen") {
// Stop current music if any
this.playbackToken++;
this.playback?.stop();
this.playback = null;
return this.startScreen(source);
}
// mode === "music"
// Stop screen if active
if (this.screenPlayback || this.dependencies.screenController?.isActive()) {
this.screenPlayback?.stop();
this.screenPlayback = null;
this.activeMode = null;
}
this.assertCanStartMusic();
const resolved = await (
this.dependencies.resolveMediaSource ?? resolveMediaSource
@@ -108,10 +120,6 @@ export class MediaController {
);
}
if (this.screenPlayback || this.dependencies.screenController?.isActive()) {
throw new AppError("Another media mode is active", "MEDIA_BUSY", 409);
}
if (this.dependencies.isBrowserStreaming?.()) {
throw new AppError(
"Stop browser microphone streaming before playing media",
@@ -122,14 +130,6 @@ export class MediaController {
}
private async startScreen(source: string): Promise<MediaState> {
if (
this.screenPlayback ||
this.dependencies.screenController?.isActive() ||
this.playback ||
this.queueStore.snapshot().current
) {
throw new AppError("Another media mode is active", "MEDIA_BUSY", 409);
}
const screenController = this.dependencies.screenController;
if (!screenController) {
throw new AppError(