Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/ui/stream/useEventStream.ts
iris bbdd32c2e0 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.
2026-11-26 01:02:34 +01:00

61 lines
2.1 KiB
TypeScript

// One `EventSource` for a page: `{ connection, reconnect }`. The three live
// feeds (the agent terminal, an agent's turn-state header and the message
// list) differ only in what they do with a message, so the state machine, the
// close on cleanup, the session probe and `reconnect` live here once.
//
// `setup` runs each time the stream is opened (a new `url`, a new `deps`, or
// `reconnect`) and returns what to do with the stream: reset the page's data
// there, and return `onMessage` (and `onOpen`, and `dispose` for a timer or a
// frame the page keeps). A `null` url opens nothing.
import { useEffect, useState } from "preact/hooks";
import { probeSession } from "../session/session.js";
import { openStream, type ConnectionState } from "./connection.js";
export type { ConnectionState };
export interface StreamHandlers {
onMessage: (data: string) => void;
/** Runs when the stream opens, a reconnect included. */
onOpen?: () => void;
/** Runs when the stream closes, for a timer or a frame the page keeps. */
dispose?: () => void;
}
export function useEventStream(
url: string | null,
setup: () => StreamHandlers,
deps: readonly unknown[],
): {
connection: ConnectionState;
/** Opens the stream again, for one the browser gave up on. */
reconnect: () => void;
} {
const [connection, setConnection] = useState<ConnectionState>("connecting");
// Bumped by `reconnect`, which runs the effect below again.
const [attempt, setAttempt] = useState(0);
useEffect(() => {
setConnection("connecting");
const handlers = setup();
if (url === null) {
handlers.dispose?.();
return;
}
const close = openStream({
url,
makeSource: (u) => new EventSource(u),
onState: setConnection,
onOpen: handlers.onOpen,
onMessage: handlers.onMessage,
onGaveUp: probeSession,
});
return () => {
close();
handlers.dispose?.();
};
// `setup` is a fresh closure each render; `deps` names what it depends on.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [url, attempt, ...deps]);
return { connection, reconnect: () => setAttempt((n) => n + 1) };
}