forge
Explore
Log in

Don't you dare go Hollow

One live stream per data-live URL on a page

Browse files at this commit

2b69914ea360ed622d5dcf64be121d23e7de9462Ahmet Kaan GümüşParent: 136f946
  • feat
    • one live stream per data-live URL on a page
Show full message
✨ feat: one live stream per data-live URL on a page

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.jsmodified+52 -26
@@ -1258,43 +1258,69 @@
12581258 // `log` events append the new bytes as they come. The stream opens at the tail's data-offset, and
12591259 // each swap brings a fresh offset, so the stream is opened again after every swap. The server ends
12601260 // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it.
1261- var liveStream = null;
1262- var liveUrl = null;
1261+ // Every element with data-live names an event stream the page follows: the repo header, the
1262+ // page's own list, issue or run. One EventSource is open per distinct URL, so a page with several
1263+ // streams updates for each of them. A `changed` event re-fetches the page and swaps it in with no
1264+ // history entry and the scroll kept. A job page also has a log tail (data-live-log) whose `log`
1265+ // events append the new bytes as they come. The tail's stream opens at its data-offset, and each
1266+ // swap brings a fresh offset, so that stream is opened again after every swap. The server ends
1267+ // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it.
1268+ var liveStreams = {}; // URL (with the tail's offset) -> EventSource
12631269 var liveTimer = null; // a run list's pending refresh: bursts of events (log lines) share one
12641270
1265- function closeLive() {
1266- if (liveStream) { liveStream.close(); }
1267- liveStream = null;
1268- liveUrl = null;
1269- liveDirty = false;
1270- if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; }
1271+ // The streams the page wants now, each once: URL -> the element that names it.
1272+ function liveWanted() {
1273+ var want = {};
1274+ if (!window.EventSource) { return want; }
1275+ var els = document.querySelectorAll('[data-live]');
1276+ for (var i = 0; i < els.length; i++) {
1277+ var url = els[i].getAttribute('data-live');
1278+ if (!url) { continue; }
1279+ var tail = els[i].querySelector('[data-live-log]');
1280+ if (tail) {
1281+ url += (url.indexOf('?') < 0 ? '?' : '&') + 'from=' + encodeURIComponent(tail.getAttribute('data-offset') || '0');
1282+ }
1283+ if (!want[url]) { want[url] = els[i]; }
1284+ }
1285+ return want;
12711286 }
12721287
1273- function syncLive() {
1274- var el = document.querySelector('[data-live]');
1275- var url = el ? el.getAttribute('data-live') : null;
1276- var tail = document.querySelector('[data-live-log]');
1277- if (url && tail) {
1278- url += (url.indexOf('?') < 0 ? '?' : '&') + 'from=' + encodeURIComponent(tail.getAttribute('data-offset') || '0');
1279- }
1280- if (!url || !window.EventSource) { closeLive(); return; }
1281- if (liveStream && liveUrl === url) { return; }
1282- closeLive();
1283- liveUrl = url;
1284- liveStream = new EventSource(url);
1285- liveStream.addEventListener('changed', function (e) {
1288+ function openLive(url) {
1289+ var es = new EventSource(url);
1290+ es.addEventListener('changed', function (e) {
12861291 // A job page's stream opens with the status the page already shows: nothing to re-render then.
1287- var wrap = document.querySelector('[data-live]');
1288- if (document.querySelector('[data-live-log]') && wrap && wrap.getAttribute('data-live-state') === e.data) { return; }
1292+ var job = document.querySelector('[data-live-log]');
1293+ var wrap = job ? job.closest('[data-live]') : null;
1294+ if (wrap && wrap.getAttribute('data-live-state') === e.data) { return; }
12891295 // A code view or an issue page re-renders at once (data-live-now). Events that come while a fetch
12901296 // is on the wire share the next one.
12911297 if (document.querySelector('[data-live-code], [data-live-now]')) { refreshLive(); return; }
12921298 // The run list changes on every log chunk of every job: refresh at most once a second.
1293- if (document.querySelector('[data-live-log]')) { refreshLive(); return; }
1299+ if (job) { refreshLive(); return; }
12941300 if (liveTimer) { return; }
12951301 liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000);
12961302 });
1297- liveStream.addEventListener('log', function (e) { appendLog(e.data); });
1303+ es.addEventListener('log', function (e) { appendLog(e.data); });
1304+ return es;
1305+ }
1306+
1307+ // Closes the streams the page no longer names and opens the ones it newly names.
1308+ function syncLive() {
1309+ var want = liveWanted();
1310+ Object.keys(liveStreams).forEach(function (url) {
1311+ if (!want[url]) { liveStreams[url].close(); delete liveStreams[url]; }
1312+ });
1313+ Object.keys(want).forEach(function (url) {
1314+ if (!liveStreams[url]) { liveStreams[url] = openLive(url); }
1315+ });
1316+ if (!Object.keys(liveStreams).length) { closeLive(); }
1317+ }
1318+
1319+ function closeLive() {
1320+ Object.keys(liveStreams).forEach(function (url) { liveStreams[url].close(); });
1321+ liveStreams = {};
1322+ liveDirty = false;
1323+ if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; }
12981324 }
12991325
13001326 // Appends streamed log text to the tail as a text node, never as HTML. The tail follows the end
@@ -1311,7 +1337,7 @@
13111337 // A change re-renders the current page. It waits while a navigation is on the wire, since that
13121338 // response could be superseded: the change is kept and sent after that request completes.
13131339 function refreshLive() {
1314- if (!liveStream) { return; }
1340+ if (!Object.keys(liveStreams).length) { return; }
13151341 if (userBusy > 0 || liveBusy) { liveDirty = true; return; }
13161342 liveDirty = false;
13171343 liveBusy = true;