From 6a0f1d7d3f20b09f7f059fc2d3183491805a1fec Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 13 Jun 2025 22:18:05 -0400 Subject: [PATCH] Realtime now playing --- src/lib/components/Album.svelte | 23 ++- src/lib/components/StreamStatus.svelte | 78 ++++++++++ src/lib/stores/now-playing-stream.ts | 133 ++++++++++++++++ src/routes/about/+page.svelte | 3 + src/routes/api/lastfm/stream/+server.ts | 196 ++++++++++++++++++++++++ 5 files changed, 426 insertions(+), 7 deletions(-) create mode 100644 src/lib/components/StreamStatus.svelte create mode 100644 src/lib/stores/now-playing-stream.ts create mode 100644 src/routes/api/lastfm/stream/+server.ts diff --git a/src/lib/components/Album.svelte b/src/lib/components/Album.svelte index b4e1b4c..3844195 100644 --- a/src/lib/components/Album.svelte +++ b/src/lib/components/Album.svelte @@ -2,6 +2,7 @@ import { spring } from 'svelte/motion' import type { Album } from '$lib/types/lastfm' import { audioPreview } from '$lib/stores/audio-preview' + import { nowPlayingStream } from '$lib/stores/now-playing-stream' import NowPlaying from './NowPlaying.svelte' interface AlbumProps { @@ -81,16 +82,24 @@ const hasPreview = $derived(!!album?.appleMusicData?.previewUrl) - // Debug log + // Subscribe to real-time now playing updates + let realtimeNowPlaying = $state<{ isNowPlaying: boolean; nowPlayingTrack?: string } | null>(null) + $effect(() => { if (album) { - console.log(`Album ${album.name}:`, { - hasAppleMusicData: !!album.appleMusicData, - previewUrl: album.appleMusicData?.previewUrl, - hasPreview + const unsubscribe = nowPlayingStream.isAlbumPlaying.subscribe(checkAlbum => { + const status = checkAlbum(album.artist.name, album.name) + if (status !== null) { + realtimeNowPlaying = status + } }) + return unsubscribe } }) + + // Combine initial state with real-time updates + const isNowPlaying = $derived(realtimeNowPlaying?.isNowPlaying ?? album?.isNowPlaying ?? false) + const nowPlayingTrack = $derived(realtimeNowPlaying?.nowPlayingTrack ?? album?.nowPlayingTrack)
@@ -110,8 +119,8 @@ style="transform: scale({$scale})" loading="lazy" /> - {#if album.isNowPlaying} - + {#if isNowPlaying} + {/if} {#if hasPreview && isHovering}