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:
parent
7f8d6abc58
commit
bbdd32c2e0
2 changed files with 294 additions and 138 deletions
|
|
@ -12,3 +12,41 @@ const CONNECTING = 0;
|
|||
export function connectionAfterError(readyState: number): ConnectionState {
|
||||
return readyState === CONNECTING ? "reconnecting" : "closed";
|
||||
}
|
||||
|
||||
/** What `openStream` uses of an `EventSource`; a test passes a fake. */
|
||||
export interface StreamSource {
|
||||
onopen: ((e: Event) => void) | null;
|
||||
onmessage: ((e: MessageEvent) => void) | null;
|
||||
onerror: ((e: Event) => void) | null;
|
||||
readonly readyState: number;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens one stream and reports it the same way for every page. `onState` hears
|
||||
* "open" when the stream opens, and "reconnecting" or "closed" after an error
|
||||
* (see `connectionAfterError`); `onGaveUp` runs when the browser gave up, so a
|
||||
* page can ask whether the session ended, which an `EventSource` does not
|
||||
* tell. Returns the function that closes it.
|
||||
*/
|
||||
export function openStream(opts: {
|
||||
url: string;
|
||||
makeSource: (url: string) => StreamSource;
|
||||
onState: (state: ConnectionState) => void;
|
||||
onOpen?: () => void;
|
||||
onMessage: (data: string) => void;
|
||||
onGaveUp?: () => void;
|
||||
}): () => void {
|
||||
const source = opts.makeSource(opts.url);
|
||||
source.onopen = () => {
|
||||
opts.onState("open");
|
||||
opts.onOpen?.();
|
||||
};
|
||||
source.onmessage = (e) => opts.onMessage(String(e.data));
|
||||
source.onerror = () => {
|
||||
const state = connectionAfterError(source.readyState);
|
||||
opts.onState(state);
|
||||
if (state === "closed") opts.onGaveUp?.();
|
||||
};
|
||||
return () => source.close();
|
||||
}
|
||||
|
|
|
|||
61
frontend/packages/swarm-ui/src/ui/stream/useEventStream.ts
Normal file
61
frontend/packages/swarm-ui/src/ui/stream/useEventStream.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// 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) };
|
||||
}
|
||||
Loading…
Reference in a new issue