Every useLive() call opened its own EventSource, and the library page and a model's Jobs tab call it twice. Over plain HTTP a browser allows 6 connections per host, so with three library tabs open the third took ~10 s to load and its requests never got an answer; a fourth tab didn't load. - /api/live answers one short request with the job snapshot (left out when the client already has that version) and, for job pages, new log lines. - src/lib/live-store.ts runs one poller per query per tab, shared by every component showing it, paused while the tab is hidden, with backoff on errors. One router.refresh() per finished job instead of one per component. - The worker dot reads the status from that poll; it only asks /api/status (every 10 s) when nothing else is polled. - The worker status no longer carries the heartbeat time (it changed every 5 s, so every idle stream re-sent its snapshot) and rounds free space to MiB. - Removes /api/stream: its sleep() added an abort listener per second to the request signal and never removed it. - Job actions poll right away after starting or cancelling a job; drops approveJob/rejectJob, whose routes went away in 0.15. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
34 lines
1.5 KiB
TypeScript
34 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { useCallback, useSyncExternalStore } from "react";
|
|
import type { LiveQuery } from "@/lib/live-protocol";
|
|
import { EMPTY_LIVE, type LiveState, liveStore } from "@/lib/live-store";
|
|
import type { WorkerStatus } from "@/lib/worker-status";
|
|
|
|
/**
|
|
* Live job state for a query (see src/lib/live-store.ts). Components showing the same query share
|
|
* one poller. When a job reaches a terminal state, server components are refreshed (model status,
|
|
* library).
|
|
*/
|
|
export function useLive(query: LiveQuery = {}, withLogs = false): LiveState {
|
|
const router = useRouter();
|
|
const { model, job } = query;
|
|
const subscribe = useCallback(
|
|
(listener: () => void) => liveStore().subscribe({ model, job }, withLogs, listener, () => router.refresh()),
|
|
[model, job, withLogs, router],
|
|
);
|
|
const getSnapshot = useCallback(() => liveStore().get({ model, job }, withLogs), [model, job, withLogs]);
|
|
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
}
|
|
|
|
const getServerSnapshot = () => EMPTY_LIVE;
|
|
const subscribeWorker = (listener: () => void) => liveStore().subscribeWorker(listener);
|
|
const getWorker = () => liveStore().getWorker();
|
|
const noWorker = () => null;
|
|
|
|
/** The worker status: from the live poll when one runs, otherwise /api/status every 10 s. */
|
|
export function useWorkerStatus(initial: WorkerStatus): WorkerStatus {
|
|
return useSyncExternalStore(subscribeWorker, getWorker, noWorker) ?? initial;
|
|
}
|