The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Notifications: a hidden tab keeps its stream until the finished job has settled
Claude5200bfd71cbe
diff --git a/src/components/job-notifications.tsx b/src/components/job-notifications.tsxindex bf627cac..e94d13ef 100644--- a/src/components/job-notifications.tsx+++ b/src/components/job-notifications.tsx@@ -8,6 +8,7 @@ import { anotherTabVisible, keepStreamWhileHidden, leadsStream, useGenerationEveimport type { Generation } from "@/lib/catalog/types";import {dueJobs,+ keepListening,longestRunning,notificationText,runningJobs,@@ -82,14 +83,23 @@ export function JobNotifications() {[router],);+ const notifying = useRef(0);const notify = useCallback(async (job: FinishedJob) => {- if (!notificationsEnabled() || !leadsStream() || document.visibilityState !== "hidden") return;- const otherVisible = await anotherTabVisible();+ // Whether this tab heard the job finish as the stream's reader: asked before the wait below.+ const leader = leadsStream();+ if (!notificationsEnabled() || !leader || document.visibilityState !== "hidden") return;+ notifying.current++;+ let otherVisible: boolean;+ try {+ otherVisible = await anotherTabVisible();+ } finally {+ notifying.current--;+ }const ok = shouldNotify({enabled: notificationsEnabled(),permission: notificationPermission(),- leader: leadsStream(),+ leader,hidden: document.visibilityState === "hidden",otherVisible,});@@ -153,9 +163,10 @@ export function JobNotifications() {return () => clearInterval(t);}, [busy, check]);- // Hidden for a while, a tab lets go of the stream; with notifications on it keeps listening while a job runs.+ // Hidden for a while, a tab lets go of the stream; with notifications on it keeps listening until+ // every job it watches has had its notification (one that just finished is still settling).useEffect(() => {- keepStreamWhileHidden(() => notificationsEnabled() && Object.values(watch.current).some((j) => j.quietSince == null));+ keepStreamWhileHidden(() => keepListening({ enabled: notificationsEnabled(), watched: watch.current, notifying: notifying.current }));return () => keepStreamWhileHidden(null);}, []);diff --git a/src/lib/api.ts b/src/lib/api.tsindex 2b21e073..c3ab473f 100644--- a/src/lib/api.ts+++ b/src/lib/api.ts@@ -140,7 +140,7 @@ interface ClientStreamState {hiddenTimer: ReturnType<typeof setTimeout> | null;stopTimer: ReturnType<typeof setTimeout> | null;hooked: boolean;- /** Keep the stream (or our place in line) while hidden: browser notifications are on and a job runs. */+ /** Keep the stream (or our place in line) while hidden: browser notifications are on and a job hasn't had its notification yet. */keepHidden: (() => boolean) | null;/** "Anyone on screen?" questions waiting for an answer, by id. */seen: Map<string, () => void>;@@ -329,7 +329,7 @@ function onVisibility() {if (document.visibilityState === "hidden") {const arm = () =>setTimeout(() => {- // Notifications on and a job running: the stream is how we hear it finish, so keep it.+ // Notifications on and a job not yet notified (running, or finished and settling): the stream is how we hear it, so keep it.if (s.keepHidden?.()) return void (s.hiddenTimer = arm());s.hiddenTimer = null;stop();diff --git a/src/lib/job-watch.ts b/src/lib/job-watch.tsindex af64a8ad..30e2c6ea 100644--- a/src/lib/job-watch.ts+++ b/src/lib/job-watch.ts@@ -129,6 +129,16 @@ export function dueJobs(state: WatchState, now: number, settle = SETTLE_MS): { sreturn { state: next, done };}+/**+ * Whether a hidden tab keeps the live stream past its 30 s grace: notifications are on and a job it+ * watches hasn't been told about yet, still running or finished but not settled (letting go then+ * would lose the job's notification: only a tab that reads the stream sends one), or a notification+ * is on its way (asking the other tabs whether one is on screen).+ */+export function keepListening(s: { enabled: boolean; watched: WatchState; notifying: number }): boolean {+ return s.enabled && (Object.keys(s.watched).length > 0 || s.notifying > 0);+}+/** The longest any watched job has been running (for the one-time offer). */export function longestRunning(state: WatchState, now: number): number {return Math.max(0, ...Object.values(state).filter((j) => j.quietSince == null).map((j) => now - j.since));diff --git a/tests/e2e/notifications.mjs b/tests/e2e/notifications.mjsindex 189393c2..e11ee373 100644--- a/tests/e2e/notifications.mjs+++ b/tests/e2e/notifications.mjs@@ -7,7 +7,8 @@//// 1. The title shows "(1) …" while a job runs and drops it when it's done.// 2. A job finishing while the tab is hidden → one notification ("Your image is ready" + its prompt);-// clicking it opens the result in the viewer.+// clicking it opens the result in the viewer. Also when it finishes just before the hidden tab's+// 30 s grace runs out (the clock held there): the tab keeps its stream until the job has settled.// 3. A job finishing while you look at the tab → no notification.// 4. The leader tab hidden while another tab is on screen → none; two hidden tabs → one (only the// stream's leader notifies).@@ -66,6 +67,12 @@ const start = (page, prompt) =>const shown = (page) => page.evaluate(() => window.__shown.map((n) => ({ title: n.title, body: n.body, tag: n.tag })));+/** Poll in real time (the page's own clock may be held). */+async function until(check, what, ms = 30_000) {+ for (const end = Date.now() + ms; Date.now() < end; await new Promise((r) => setTimeout(r, 250))) if (await check()) return;+ throw new Error(`timed out waiting for ${what}`);+}+try {{const ctx = await browser.newContext(DESKTOP);@@ -101,6 +108,31 @@ try {await page.keyboard.press("Escape");await ctx.close();}+ {+ step("finished 3 s before the hidden tab's 30 s grace ends: the tab keeps the stream until it settles, then notifies");+ const ctx = await browser.newContext(DESKTOP);+ await ctx.addInitScript(...fakes());+ const page = await ctx.newPage();+ await page.clock.install();+ await openRoute(page, `${app}/library`);+ await page.clock.runFor(1500);+ await page.evaluate(() => window.__setHidden(true));+ const hiddenAt = await page.evaluate(() => Date.now());+ // 27 s into the grace, with the clock held there while the job runs (in real time): it goes quiet+ // 3 s before the grace ends and is due (settled) 3 s after.+ await page.clock.pauseAt(hiddenAt + 27_000);+ const id = await start(page, "just before the grace runs out");+ await until(() => page.evaluate((id) => fetch(`/api/generations/${id}`).then((r) => r.json()).then((g) => g.status === "succeeded"), id), "the job to finish");+ await until(() => page.evaluate(() => !/^\(\d+\) /.test(document.title)), "the tab to hear it finish");+ await page.clock.runFor(3_500);+ assert.deepEqual(await shown(page), [], "nothing before the job has settled");+ await page.clock.runFor(4_000);+ await until(() => shown(page).then((l) => l.length > 0), "the notification", 5_000);+ const list = await shown(page);+ assert.equal(list.length, 1, JSON.stringify(list));+ assert.deepEqual([list[0].title, list[0].body], ["Your image is ready", "just before the grace runs out"]);+ await ctx.close();+ }{step("the leader tab hidden but another Fuobe tab on screen: no notification");const ctx = await browser.newContext(DESKTOP);diff --git a/tests/job-watch.test.ts b/tests/job-watch.test.tsindex a7ba4d40..95f5a869 100644--- a/tests/job-watch.test.ts+++ b/tests/job-watch.test.ts@@ -3,6 +3,7 @@ import type { Generation } from "@/lib/catalog/types";import type { StreamEvent } from "@/lib/generation-stream";import {dueJobs,+ keepListening,longestRunning,notificationText,OFFER_AFTER_MS,@@ -129,6 +130,27 @@ describe("a finished job (watchEvent → dueJobs)", () => {expect(s).toEqual({});});+ it("keeps a hidden tab's stream until every watched job has had its notification, settling ones too", () => {+ const g = gen();+ let s = play({}, [+ [g, 0],+ [{ ...g, status: "running" }, 1000],+ ]);+ expect(keepListening({ enabled: true, watched: s, notifying: 0 })).toBe(true);+ // Finished 2 s before the 30 s grace runs out: quiet but not due for another 4 s. Letting go+ // now would lose its notification (only a tab reading the stream sends one).+ s = watchEvent(s, ev({ ...g, status: "succeeded", outputs: [{ url: "/api/files/o.mp4", kind: "video" }] }), 28_000);+ expect(dueJobs(s, 30_000).done).toEqual([]);+ expect(keepListening({ enabled: true, watched: s, notifying: 0 })).toBe(true);+ const { state, done } = dueJobs(s, 28_000 + SETTLE_MS);+ expect(done).toHaveLength(1);+ // While it asks the other tabs whether one is on screen, still listening; then it lets go.+ expect(keepListening({ enabled: true, watched: state, notifying: 1 })).toBe(true);+ expect(keepListening({ enabled: true, watched: state, notifying: 0 })).toBe(false);+ // Notifications off: the usual grace, whatever runs.+ expect(keepListening({ enabled: false, watched: s, notifying: 1 })).toBe(false);+ });+it("watches what was already running when the page opened", () => {const running = [gen({ status: "running" }), gen({ status: "queued", batchId: "b" })];const s = seedWatch({}, running, 0);