Refactor song polling logic for adaptive delays and add tsconfig.json for TypeScript configuration
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m23s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m23s
This commit is contained in:
parent
6714a4763a
commit
84f9502b4a
2 changed files with 66 additions and 31 deletions
|
@ -1,4 +1,4 @@
|
|||
import { Elysia, t } from 'elysia';
|
||||
import { Elysia } from 'elysia';
|
||||
import { staticPlugin } from '@elysiajs/static';
|
||||
import path from 'path';
|
||||
|
||||
|
@ -15,6 +15,7 @@ interface LastSong {
|
|||
// Current song state
|
||||
let currentSong: LastSong | null = null;
|
||||
let lastSongId: string | null = null;
|
||||
let pollTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
// WebSocket connections
|
||||
const connections = new Set<any>();
|
||||
|
@ -54,18 +55,20 @@ async function fetchLastFmSong(): Promise<LastSong | null> {
|
|||
async function checkAndUpdateSong(): Promise<void> {
|
||||
const newSong = await fetchLastFmSong();
|
||||
|
||||
if (!newSong) return;
|
||||
// Default to not currently playing if we couldn't fetch anything
|
||||
let nowPlaying = false;
|
||||
|
||||
// Create a unique identifier for the song
|
||||
if (newSong) {
|
||||
// Create a unique identifier for the song (include nowPlaying state)
|
||||
const newSongId = `${newSong.artist}-${newSong.name}-${newSong.nowPlaying}`;
|
||||
|
||||
// Check if song has changed
|
||||
if (newSongId !== lastSongId) {
|
||||
console.log('Song changed:', newSong.name, 'by', newSong.artist);
|
||||
// Update state and notify clients
|
||||
currentSong = newSong;
|
||||
lastSongId = newSongId;
|
||||
|
||||
// Notify all connected WebSocket clients
|
||||
const message = JSON.stringify({
|
||||
type: 'song-update',
|
||||
data: newSong
|
||||
|
@ -79,13 +82,24 @@ async function checkAndUpdateSong(): Promise<void> {
|
|||
connections.delete(ws);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Keep currentSong updated even if no change to ID (e.g., image/url may vary)
|
||||
currentSong = newSong;
|
||||
}
|
||||
|
||||
nowPlaying = !!newSong.nowPlaying;
|
||||
} else {
|
||||
// If fetch failed, keep existing currentSong and fall back to slower polling
|
||||
nowPlaying = currentSong?.nowPlaying ?? false;
|
||||
}
|
||||
|
||||
// Schedule next poll based on whether a track is currently playing
|
||||
const delayMs = nowPlaying ? 5000 : 30000;
|
||||
if (pollTimer) clearTimeout(pollTimer);
|
||||
pollTimer = setTimeout(checkAndUpdateSong, delayMs);
|
||||
}
|
||||
|
||||
// Start polling Last.fm every second
|
||||
setInterval(checkAndUpdateSong, 1000);
|
||||
|
||||
// Initial fetch
|
||||
// Kick off adaptive polling with an immediate check
|
||||
checkAndUpdateSong();
|
||||
|
||||
// Determine static assets path - adjust for compiled executable
|
||||
|
|
21
tsconfig.json
Normal file
21
tsconfig.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"skipLibCheck": true,
|
||||
"types": [
|
||||
"bun-types",
|
||||
"vite/client"
|
||||
],
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"vite.config.*"
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue