Live updates: one poller per tab instead of SSE streams

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
This commit is contained in:
Claude committed 2026-11-01 20:46:54 +00:00
1 parent 16c3fcc113
commit 76d851896e
11 files changed
+623 -185

No files matched your search

+5 -19
View File
@@ -1,10 +1,14 @@
"use client";
import { toast } from "sonner";
import { pokeLive } from "@/lib/live-store";
async function post(url: string): Promise<boolean> {
const res = await fetch(url, { method: "POST" });
if (res.ok) return true;
if (res.ok) {
pokeLive(); // show the new job state now instead of on the next poll
return true;
}
const body = await res.json().catch(() => ({}));
toast.error(body.error?.message ?? `Request failed (${res.status})`);
return false;
@@ -31,21 +35,3 @@ export async function startUpdate(modelId: string): Promise<boolean> {
if (ok) toast.success("Update queued");
return ok;
}
export async function approveJob(jobId: string): Promise<boolean> {
const ok = await post(`/api/jobs/${jobId}/approve`);
if (ok) toast.success("Approved; the download is queued");
return ok;
}
/** Returns whether the model left the library (it was only added by this request), or null on error. */
export async function rejectJob(jobId: string): Promise<{ removedModel: boolean } | null> {
const res = await fetch(`/api/jobs/${jobId}/reject`, { method: "POST" });
const body = await res.json().catch(() => ({}));
if (!res.ok) {
toast.error(body.error?.message ?? `Request failed (${res.status})`);
return null;
}
toast.info(body.removedModel ? "Rejected; the model was removed from the library" : "Rejected");
return body;
}
+3 -16
View File
@@ -1,25 +1,12 @@
"use client";
import { useEffect, useState } from "react";
import { Badge } from "@/components/ui/badge";
import { useWorkerStatus } from "@/hooks/use-live";
import type { WorkerStatus } from "@/lib/worker-status";
/** Polls /api/status and shows whether the background worker is alive. */
/** Shows whether the background worker is alive (from the page's live poll, or /api/status). */
export function WorkerIndicator({ initial }: { initial: WorkerStatus }) {
const [status, setStatus] = useState(initial);
useEffect(() => {
const tick = async () => {
try {
const res = await fetch("/api/status", { cache: "no-store" });
if (res.ok) setStatus((await res.json()).worker);
} catch {
// keep last known state
}
};
const id = setInterval(tick, 10_000);
return () => clearInterval(id);
}, []);
const status = useWorkerStatus(initial);
// Online: just a green dot (details on hover); offline stays spelled out.
if (status.online) {