Files
archie/ui/src/lib/stream-state.ts
T
sam 7ba45c2117 fix(webui): the live stream reopens after a fatal close; quieter connection status
A non-200 answer to /api/stream (the backend restarting behind a proxy)
closes an EventSource for good, so the top bar read Disconnected until a
reload while every fetch worked. The live-updates store now reopens it
after 1s, doubling to a 30s cap, and resets once live. The connection
status becomes a dot and a label, coloured only when not connected.

History drops the edge fade, which washed out its first and last columns
though nothing overflowed, and its section filter names the section
rather than showing the raw kind.
2026-09-25 17:45:34 +10:00

34 lines
1.3 KiB
TypeScript

// What an EventSource error actually means.
//
// EventSource fires onerror both for a dropped connection it will retry and
// for one it has given up on. Reporting "reconnecting" for both left the logs
// page showing a permanent amber "reconnecting" pill against a stream that had
// stopped for good, next to text claiming live logs were still arriving
// (archie-core-c5bz).
//
// readyState is the difference: CONNECTING means a retry is scheduled, CLOSED
// means the browser will not try again.
/** The state the UI reports for an event stream. */
export type StreamState = "live" | "reconnecting" | "unavailable";
/**
* streamStateFor maps an EventSource readyState to the state the UI reports:
* "live", "reconnecting", or "unavailable".
*/
export function streamStateFor(readyState: number): StreamState {
// Numeric literals rather than EventSource.* so this is testable without one.
if (readyState === 0) return "reconnecting";
if (readyState === 1) return "live";
return "unavailable";
}
/**
* reconnectDelay is how long to wait before re-opening a stream the browser
* gave up on (a non-200 answer closes an EventSource for good): doubling from
* one second, capped at thirty.
*/
export function reconnectDelay(attempt: number): number {
return Math.min(1000 * 2 ** attempt, 30_000);
}