Skip to content

Commit 8414ebc

Browse files
committed
Fix radio queue
1 parent 0bb7cef commit 8414ebc

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

features/radio/router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ export function createRadioRouter() {
4747
res.setHeader("Content-Type", "text/event-stream");
4848
res.setHeader("Cache-Control", "no-cache, no-transform");
4949
res.setHeader("Connection", "keep-alive");
50+
res.setHeader("X-Accel-Buffering", "no");
5051
res.flushHeaders?.();
5152
addRadioClient(tenantId, res);
53+
const heartbeat = setInterval(() => {
54+
res.write(": heartbeat\n\n");
55+
}, 25_000);
56+
res.on("close", () => clearInterval(heartbeat));
5257
const state = await getRadioState({ tenantId: baseTenantId, station });
5358
res.write(`event: state\n`);
5459
res.write(`data: ${JSON.stringify(state)}\n\n`);

features/radio/service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ export async function getRadioState({
369369
}) {
370370
const normalizedTenantId = resolvedTenantId(tenantId);
371371
const session = await ensureRadioSession(normalizedTenantId, station);
372+
const startedAtMs = session.startedAt?.getTime() ?? 0;
373+
if (
374+
session.currentTrackId &&
375+
startedAtMs > 0 &&
376+
Date.now() >= startedAtMs + session.durationSeconds * 1000
377+
) {
378+
await advanceRadioIfNeeded(normalizedTenantId, false, station);
379+
const advancedSession = await ensureRadioSession(normalizedTenantId, station);
380+
return presentRadioState(advancedSession, actor);
381+
}
372382
return presentRadioState(session, actor);
373383
}
374384

0 commit comments

Comments
 (0)