From 2b69914ea360ed622d5dcf64be121d23e7de9462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 22 Oct 2026 03:24:03 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20one=20live=20stream=20per?= =?UTF-8?q?=20data-live=20URL=20on=20a=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nav.js opens an EventSource for every distinct data-live URL in the document, not only the first one, so the repo header counts and the page's own list, issue or run all update together. --- crates/forge-web/assets/nav.js | 78 ++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 59959bd..4172d83 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -1258,43 +1258,69 @@ // `log` events append the new bytes as they come. The stream opens at the tail's data-offset, and // each swap brings a fresh offset, so the stream is opened again after every swap. The server ends // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it. - var liveStream = null; - var liveUrl = null; + // Every element with data-live names an event stream the page follows: the repo header, the + // page's own list, issue or run. One EventSource is open per distinct URL, so a page with several + // streams updates for each of them. A `changed` event re-fetches the page and swaps it in with no + // history entry and the scroll kept. A job page also has a log tail (data-live-log) whose `log` + // events append the new bytes as they come. The tail's stream opens at its data-offset, and each + // swap brings a fresh offset, so that stream is opened again after every swap. The server ends + // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it. + var liveStreams = {}; // URL (with the tail's offset) -> EventSource var liveTimer = null; // a run list's pending refresh: bursts of events (log lines) share one - function closeLive() { - if (liveStream) { liveStream.close(); } - liveStream = null; - liveUrl = null; - liveDirty = false; - if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; } + // The streams the page wants now, each once: URL -> the element that names it. + function liveWanted() { + var want = {}; + if (!window.EventSource) { return want; } + var els = document.querySelectorAll('[data-live]'); + for (var i = 0; i < els.length; i++) { + var url = els[i].getAttribute('data-live'); + if (!url) { continue; } + var tail = els[i].querySelector('[data-live-log]'); + if (tail) { + url += (url.indexOf('?') < 0 ? '?' : '&') + 'from=' + encodeURIComponent(tail.getAttribute('data-offset') || '0'); + } + if (!want[url]) { want[url] = els[i]; } + } + return want; } - function syncLive() { - var el = document.querySelector('[data-live]'); - var url = el ? el.getAttribute('data-live') : null; - var tail = document.querySelector('[data-live-log]'); - if (url && tail) { - url += (url.indexOf('?') < 0 ? '?' : '&') + 'from=' + encodeURIComponent(tail.getAttribute('data-offset') || '0'); - } - if (!url || !window.EventSource) { closeLive(); return; } - if (liveStream && liveUrl === url) { return; } - closeLive(); - liveUrl = url; - liveStream = new EventSource(url); - liveStream.addEventListener('changed', function (e) { + function openLive(url) { + var es = new EventSource(url); + es.addEventListener('changed', function (e) { // A job page's stream opens with the status the page already shows: nothing to re-render then. - var wrap = document.querySelector('[data-live]'); - if (document.querySelector('[data-live-log]') && wrap && wrap.getAttribute('data-live-state') === e.data) { return; } + var job = document.querySelector('[data-live-log]'); + var wrap = job ? job.closest('[data-live]') : null; + if (wrap && wrap.getAttribute('data-live-state') === e.data) { return; } // A code view or an issue page re-renders at once (data-live-now). Events that come while a fetch // is on the wire share the next one. if (document.querySelector('[data-live-code], [data-live-now]')) { refreshLive(); return; } // The run list changes on every log chunk of every job: refresh at most once a second. - if (document.querySelector('[data-live-log]')) { refreshLive(); return; } + if (job) { refreshLive(); return; } if (liveTimer) { return; } liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); }); - liveStream.addEventListener('log', function (e) { appendLog(e.data); }); + es.addEventListener('log', function (e) { appendLog(e.data); }); + return es; + } + + // Closes the streams the page no longer names and opens the ones it newly names. + function syncLive() { + var want = liveWanted(); + Object.keys(liveStreams).forEach(function (url) { + if (!want[url]) { liveStreams[url].close(); delete liveStreams[url]; } + }); + Object.keys(want).forEach(function (url) { + if (!liveStreams[url]) { liveStreams[url] = openLive(url); } + }); + if (!Object.keys(liveStreams).length) { closeLive(); } + } + + function closeLive() { + Object.keys(liveStreams).forEach(function (url) { liveStreams[url].close(); }); + liveStreams = {}; + liveDirty = false; + if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; } } // Appends streamed log text to the tail as a text node, never as HTML. The tail follows the end @@ -1311,7 +1337,7 @@ // A change re-renders the current page. It waits while a navigation is on the wire, since that // response could be superseded: the change is kept and sent after that request completes. function refreshLive() { - if (!liveStream) { return; } + if (!Object.keys(liveStreams).length) { return; } if (userBusy > 0 || liveBusy) { liveDirty = true; return; } liveDirty = false; liveBusy = true;