mirror of
https://github.com/janishutz/MusicPlayer.git
synced 2026-09-10 14:25:24 +02:00
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import {
|
|
currentQueue,
|
|
currentQueueIdx,
|
|
isPlaying,
|
|
startTime
|
|
} from './state';
|
|
import type {
|
|
Song
|
|
} from '../dtype/playlist';
|
|
|
|
interface ReceivedJSONMessage {
|
|
'type': 'state' | 'playlist';
|
|
'data': unknown;
|
|
}
|
|
|
|
export interface StateUpdate {
|
|
'playing': boolean;
|
|
'index': number;
|
|
'start': number;
|
|
}
|
|
|
|
export const messageHandler = ( msg: string ) => {
|
|
if ( msg.startsWith( 'json:' ) ) {
|
|
try {
|
|
const data = JSON.parse( msg.substring( 5 ) ) as ReceivedJSONMessage;
|
|
|
|
if ( data.type === 'playlist' ) {
|
|
currentQueue.value = ( data.data as {
|
|
'playlist': Song[]
|
|
} ?? {
|
|
'playlist': []
|
|
} ).playlist;
|
|
} else if ( data.type === 'state' ) {
|
|
const state = data.data as StateUpdate;
|
|
|
|
currentQueueIdx.value = state.index;
|
|
isPlaying.value = state.playing;
|
|
startTime.value = state.start;
|
|
} else {
|
|
console.log( '[SSE] Received unknown data', data.type );
|
|
}
|
|
} catch ( err ) {
|
|
console.error( 'JSON DECODE failed with error', err );
|
|
}
|
|
}
|
|
};
|