- proxy Go /api/stream through Nitro; drive /stage, /live, /plan and /forecast from scenario events - fall back to 5 s polling after SSE failures or heartbeat silence; show Live/Polling/Offline - authenticate /control and POST /api/scenario in Nitro with server-side control credentials - add big-screen stage with local QR and a no-login phone recommendation view - add SSE unit tests and a live smoke test; refresh README/Makefile
102 lines
3.5 KiB
TypeScript
102 lines
3.5 KiB
TypeScript
import { SCENARIOS } from './types'
|
|
import type { Scenario, ScenarioState } from './types'
|
|
|
|
/** Client connection modes shown on /stage, /live and /plan. */
|
|
export type LiveConnection = 'connecting' | 'live' | 'polling' | 'offline'
|
|
|
|
/** Poll the public scenario endpoint every 5 s while SSE is unavailable. */
|
|
export const SCENARIO_POLL_MS = 5000
|
|
/** Two consecutive SSE errors are enough to start the polling fallback. */
|
|
export const SSE_FAILURES_BEFORE_POLLING = 2
|
|
/** The Go hub pings every 15 s; two missed pings mean the stream is buffered. */
|
|
export const SSE_STALE_MS = 32000
|
|
|
|
export const LIVE_LABELS: Record<LiveConnection, string> = {
|
|
connecting: 'Connecting',
|
|
live: 'Live',
|
|
polling: 'Polling',
|
|
offline: 'Offline',
|
|
}
|
|
|
|
export const LIVE_COLORS: Record<LiveConnection, 'neutral' | 'success' | 'warning' | 'danger'> = {
|
|
connecting: 'neutral',
|
|
live: 'success',
|
|
polling: 'warning',
|
|
offline: 'danger',
|
|
}
|
|
|
|
export interface LiveStatusInput {
|
|
online: boolean
|
|
sseOpen: boolean
|
|
sseFailures: number
|
|
polling: boolean
|
|
pollFailures: number
|
|
}
|
|
|
|
/**
|
|
* Resolve the visible connection state. Live means an open SSE stream,
|
|
* Polling means the 5 s fallback is working, Offline means neither path works.
|
|
*/
|
|
export function liveStatus(input: LiveStatusInput): LiveConnection {
|
|
if (!input.online) return 'offline'
|
|
if (input.sseOpen) return 'live'
|
|
if (input.polling) return input.pollFailures > 0 ? 'offline' : 'polling'
|
|
if (input.sseFailures >= SSE_FAILURES_BEFORE_POLLING) {
|
|
return input.pollFailures > 0 ? 'offline' : 'polling'
|
|
}
|
|
return 'connecting'
|
|
}
|
|
|
|
/** Parse and validate one `event: scenario` payload from Go /api/stream. */
|
|
export function parseScenarioEvent(raw: string): ScenarioState | null {
|
|
let value: unknown
|
|
try {
|
|
value = JSON.parse(raw)
|
|
} catch {
|
|
return null
|
|
}
|
|
if (!value || typeof value !== 'object') return null
|
|
const candidate = value as Partial<ScenarioState>
|
|
if (typeof candidate.version !== 'number' || !Number.isFinite(candidate.version)) return null
|
|
if (typeof candidate.at !== 'string' || !candidate.at) return null
|
|
if (!candidate.scenario || typeof candidate.scenario !== 'object') return null
|
|
const scenario = candidate.scenario as { scenario?: unknown }
|
|
if (typeof scenario.scenario !== 'string') return null
|
|
if (!SCENARIOS.includes(scenario.scenario as Scenario)) return null
|
|
return candidate as ScenarioState
|
|
}
|
|
|
|
/** A new server version, or a changed scenario id, is a reason to refetch. */
|
|
export function scenarioChanged(
|
|
previous: ScenarioState | null | undefined,
|
|
next: ScenarioState,
|
|
): boolean {
|
|
if (!previous) return true
|
|
return previous.version !== next.version || previous.scenario.scenario !== next.scenario.scenario
|
|
}
|
|
|
|
export function scenarioLabel(scenario: Scenario | string | undefined): string {
|
|
switch (scenario) {
|
|
case 'sunny-saturday-market':
|
|
return 'Sunny Saturday + market'
|
|
case 'rainy-saturday':
|
|
return 'Rainy Saturday'
|
|
case 'weekday-event':
|
|
return 'Wednesday evening event'
|
|
default:
|
|
return 'Base week'
|
|
}
|
|
}
|
|
|
|
/** Small, dependency-free age label for the last SSE/poll event. */
|
|
export function liveAge(lastEventAt: string, now = Date.now()): string {
|
|
if (!lastEventAt) return 'no event yet'
|
|
const time = Date.parse(lastEventAt)
|
|
if (Number.isNaN(time)) return 'no event yet'
|
|
const seconds = Math.max(0, Math.round((now - time) / 1000))
|
|
if (seconds < 5) return 'just now'
|
|
if (seconds < 60) return `${seconds}s ago`
|
|
const minutes = Math.round(seconds / 60)
|
|
return `${minutes} min ago`
|
|
}
|