feat(webui): subscribe to /api/state/stream — push-driven dashboard state

Phase 4 of the SSE migration. The dashboard's three live resources now
update the moment the backend sees a change instead of within 0-3000ms of
the next polling tick.

- New api.stateStream({ onEvent, onStatus }) in lib/api/client.ts mirrors
  the existing openLogStream / openDebugStream EventSource pattern: token in
  the query string (EventSource can't set headers), single onerror →
  'reconnecting' status, source.close() on dispose, JSON.parse with skip-
  malformed isolation. Browser-native EventSource auto-reconnect covers
  transport drops with no manual retry timer.

- StateStreamEvent union covers the three resource frames plus the two
  control frames ('ready' on connect, 'dropped' on backpressure). The
  resource frames carry typed HookProcessInfo[] / QQInfo[] /
  AccountConnections[] payloads.

- AppLayout now subscribes on mount, applies pushed snapshots to the same
  setProcessList / setQqList / setConnections React state slots the polling
  tick used to drive. The REST polling tick is RETAINED but slowed to
  max(pollInterval × 10, 10s) (default 3s → 30s) so it acts as a silent-
  drop reconcile rather than the primary update path. systemInfo isn't on
  the SSE feed — it stays on the slow REST loop.

Resolves the two reported bugs:
  1. new QQ.exe not showing in WebUI until manual refresh → process-
     discovered now publishes 'processes' through the StateBus → frame
     arrives within 50ms.
  2. injection panel stuck at "waiting for connection" after a successful
     hook (group messages arriving) → HookSession 'status-changed' now
     publishes 'processes' → 'online' status frame arrives within 50ms.

WebUI typecheck clean. No unit tests in packages/webui (consistent with the
existing openLogStream/openDebugStream which also have no client tests).
This commit is contained in:
DrSmoothl committed 2026-06-24 21:51:35 +08:00
1 parent a7522deb82
commit de3bb4817d
3 files changed
+73 -1

No files matched your search

+22
View File
@@ -10,6 +10,8 @@ import {
type LoginResult,
type LogsStreamOptions,
type ProcessActionResult,
type StateStreamEvent,
type StateStreamOptions,
type TokenStore,
} from './types';
import { localStorageTokenStore } from './token-store';
@@ -344,6 +346,26 @@ class HttpApiClient implements ApiClient {
return this.getJson<{ list: AccountConnections[] }>('/api/connections').then((d) => d.list ?? []);
}
stateStream(options: StateStreamOptions): () => void {
if (!this.currentToken) { options.onStatus?.('closed'); return () => {}; }
const url = `/api/state/stream?token=${encodeURIComponent(this.currentToken)}`;
const source = new EventSource(url);
// EventSource auto-reconnects on transport drop: `onerror` fires once
// when the connection is lost and the browser is about to retry, so
// a single 'reconnecting' status is sufficient — no manual reconnect
// timer needed.
source.onopen = () => options.onStatus?.('open');
source.onerror = () => options.onStatus?.('reconnecting');
source.onmessage = (event) => {
try {
options.onEvent(JSON.parse(event.data) as StateStreamEvent);
} catch {
/* malformed frame — skip; the next one will arrive normally */
}
};
return () => { source.close(); options.onStatus?.('closed'); };
}
// ---------- SSE ----------
private openLogStream(options: LogsStreamOptions): () => void {