mirror of
https://github.com/janishutz/MusicPlayer.git
synced 2026-09-10 14:25:24 +02:00
feat: basic rendering of queue on share screen
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user