import { PlaybackStateBridge } from './PlaybackStateBridge' import { VideoItem } from '../viewmodel/VideoItem' export interface PlaybackRuntime { playQueue: (queue: VideoItem[], startIndex: number, source: string) => Promise playOrPause: () => Promise playNext: () => Promise playPrevious: () => Promise seekTo: (value: string, source?: string) => Promise } export class PlaybackCoordinator { private static instance?: PlaybackCoordinator private runtime?: PlaybackRuntime private stateBridge: PlaybackStateBridge = PlaybackStateBridge.getInstance() public static getInstance(): PlaybackCoordinator { if (!PlaybackCoordinator.instance) { PlaybackCoordinator.instance = new PlaybackCoordinator() } return PlaybackCoordinator.instance } public setRuntime(runtime: PlaybackRuntime): void { this.runtime = runtime } public clearRuntime(runtime?: PlaybackRuntime): void { if (!runtime || this.runtime === runtime) { this.runtime = undefined } } public async playQueue(queue: VideoItem[], startIndex: number, source: string): Promise { if (!this.runtime || queue.length <= 0) { return } this.stateBridge.replaceQueue(queue, startIndex) await this.runtime.playQueue(queue, this.stateBridge.getSnapshot().currentIndex, source) } public async playSong(song: VideoItem, source: string): Promise { await this.playQueue([song], 0, source) } public async playOrPause(): Promise { await this.runtime?.playOrPause() } public async playNext(): Promise { await this.runtime?.playNext() } public async playPrevious(): Promise { await this.runtime?.playPrevious() } public async seekTo(value: string, source?: string): Promise { await this.runtime?.seekTo(value, source) } }