Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: one stream hook, and one rule for confirming a change

Part of the unification order in the UI sweep issue (steps e and f).

The terminal, turn-state and message feeds each opened an EventSource, tracked
connecting/open/reconnecting/closed, probed the session when the browser gave
up, and reconnected through an attempt counter. openStream (no Preact, source
injected, tested with a fake) and useEventStream now own that; each feed keeps
only what it does with a message.

A change confirmed itself three ways: a toast, inline, or nothing. The design
guide now says forms confirm inline, actions on a card in a refreshing list
confirm with a toast, and a change the page already shows says nothing. Fire
now, saving a schedule and removing an egress entry gain the toast they lacked.
This commit is contained in:
iris 2026-11-26 00:57:53 +01:00
commit bbdd32c2e0

View file

@ -6,14 +6,13 @@
// resumes a reconnect, so rows are kept across one. Rows that arrive within
// 50 ms are folded in together, and `term-rows.ts` holds the rules for that:
// coalesce by key, and keep the newest `MAX_ROWS`.
import { useEffect, useRef, useState } from "preact/hooks";
import { useRef, useState } from "preact/hooks";
import type { TermMsg, TermRow } from "@hive/shared/term-msg.js";
import {
connectionAfterError,
useEventStream,
type ConnectionState,
} from "../../ui/stream/connection.js";
import { probeSession } from "../../ui/session/session.js";
} from "../../ui/stream/useEventStream.js";
import { appendRows } from "./term-rows.js";
export type { ConnectionState };
@ -33,14 +32,10 @@ export function useSwarmTermStream(
streamUrl: string,
): UseSwarmTermStreamResult {
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(() => {
const { connection, reconnect } = useEventStream(streamUrl, () => {
setRows([]);
setConnection("connecting");
keySeqRef.current = 0;
let batch: TermRow[] = [];
let timer: ReturnType<typeof setTimeout> | null = null;
@ -54,36 +49,29 @@ export function useSwarmTermStream(
batch.push(row);
if (timer === null) timer = setTimeout(flush, BATCH_MS);
}
const es = new EventSource(streamUrl);
es.onopen = () => setConnection("open");
es.onmessage = (e) => {
keySeqRef.current += 1;
const key = "r" + keySeqRef.current;
let msg: TermMsg;
try {
msg = JSON.parse(e.data);
} catch {
push({
key,
level: "warn",
fromHistory: false,
summary: "[parse err] " + e.data,
});
return;
}
push({ ...msg, key, fromHistory: false });
return {
onMessage: (data) => {
keySeqRef.current += 1;
const key = "r" + keySeqRef.current;
let msg: TermMsg;
try {
msg = JSON.parse(data);
} catch {
push({
key,
level: "warn",
fromHistory: false,
summary: "[parse err] " + data,
});
return;
}
push({ ...msg, key, fromHistory: false });
},
dispose: () => {
if (timer !== null) clearTimeout(timer);
},
};
es.onerror = () => {
const state = connectionAfterError(es.readyState);
setConnection(state);
// The browser gave up: say whether the session ended, which `EventSource` does not tell.
if (state === "closed") probeSession();
};
return () => {
es.close();
if (timer !== null) clearTimeout(timer);
};
}, [streamUrl, attempt]);
}, []);
return { rows, connection, reconnect: () => setAttempt((n) => n + 1) };
return { rows, connection, reconnect };
}