Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: let a closed stream reconnect and a failed caps read retry

When the server answers an event stream with an error status the browser
gives up and the badge stayed on disconnected with nothing to press. The
terminal, its state header and the messages page now offer a reconnect,
and the one rule for the state after an error moved into a tested helper.
The resource caps panel gets a retry beside its error, and its loading
state announces itself.
This commit is contained in:
iris 2026-10-21 23:16:04 +02:00 • committed by mara
commit 31b7452e56

View file

@ -12,7 +12,12 @@
import { useEffect, useRef, useState } from "preact/hooks";
import type { TermMsg, TermRow } from "@hive/shared/term-msg.js";
export type ConnectionState = "connecting" | "open" | "reconnecting" | "closed";
import {
connectionAfterError,
type ConnectionState,
} from "../../ui/stream/connection.js";
export type { ConnectionState };
function appendRow(rows: TermRow[], row: TermRow): TermRow[] {
const last = rows[rows.length - 1];
@ -25,6 +30,8 @@ function appendRow(rows: TermRow[], row: TermRow): TermRow[] {
export interface UseSwarmTermStreamResult {
rows: TermRow[];
connection: ConnectionState;
/** Opens the stream again, for one the browser gave up on. */
reconnect: () => void;
}
export function useSwarmTermStream(
@ -33,6 +40,8 @@ export function useSwarmTermStream(
const [rows, setRows] = useState<TermRow[]>([]);
const [connection, setConnection] = useState<ConnectionState>("connecting");
const keySeqRef = useRef(0);
// Bumped by `reconnect`, which reruns the effect below.
const [attempt, setAttempt] = useState(0);
useEffect(() => {
setRows([]);
@ -59,13 +68,9 @@ export function useSwarmTermStream(
}
setRows((prev) => appendRow(prev, { ...msg, key, fromHistory: false }));
};
es.onerror = () => {
setConnection(
es.readyState === 0 /* CONNECTING */ ? "reconnecting" : "closed",
);
};
es.onerror = () => setConnection(connectionAfterError(es.readyState));
return () => es.close();
}, [streamUrl]);
}, [streamUrl, attempt]);
return { rows, connection };
return { rows, connection, reconnect: () => setAttempt((n) => n + 1) };
}