One live stream per data-live URL on a page
Browse files at this commit2b69914ea360ed622d5dcf64be121d23e7de9462Parent: 136f946- feat
- one live stream per data-live URL on a page
1 changed file +52 -26
- crates/forge-web/assets/nav.js +52 -26
crates/forge-web/assets/nav.jsmodified+52 -26
| @@ -1258,43 +1258,69 @@ | |||
| 1258 | // `log` events append the new bytes as they come. The stream opens at the tail's data-offset, and | 1258 | // `log` events append the new bytes as they come. The stream opens at the tail's data-offset, and |
| 1259 | // each swap brings a fresh offset, so the stream is opened again after every swap. The server ends | 1259 | // each swap brings a fresh offset, so the stream is opened again after every swap. The server ends |
| 1260 | // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it. | 1260 | // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it. |
| 1261 | - var liveStream = null; | 1261 | + // Every element with data-live names an event stream the page follows: the repo header, the |
| 1262 | - var liveUrl = null; | 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 | ||
| 1263 | var liveTimer = null; // a run list's pending refresh: bursts of events (log lines) share one | 1269 | var liveTimer = null; // a run list's pending refresh: bursts of events (log lines) share one |
| 1264 | 1270 | ||
| 1265 | - function closeLive() { | 1271 | + // The streams the page wants now, each once: URL -> the element that names it. |
| 1266 | - if (liveStream) { liveStream.close(); } | 1272 | + function liveWanted() { |
| 1267 | - liveStream = null; | 1273 | + var want = {}; |
| 1268 | - liveUrl = null; | 1274 | + if (!window.EventSource) { return want; } |
| 1269 | - liveDirty = false; | 1275 | + var els = document.querySelectorAll('[data-live]'); |
| 1270 | - if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; } | 1276 | + for (var i = 0; i < els.length; i++) { |
| 1271 | - } | 1277 | + var url = els[i].getAttribute('data-live'); |
| 1272 | - | 1278 | + if (!url) { continue; } |
| 1273 | - function syncLive() { | 1279 | + var tail = els[i].querySelector('[data-live-log]'); |
| 1274 | - var el = document.querySelector('[data-live]'); | 1280 | + if (tail) { |
| 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'); | 1281 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'from=' + encodeURIComponent(tail.getAttribute('data-offset') || '0'); |
| 1279 | } | 1282 | } |
| 1280 | - if (!url || !window.EventSource) { closeLive(); return; } | 1283 | + if (!want[url]) { want[url] = els[i]; } |
| 1281 | - if (liveStream && liveUrl === url) { return; } | 1284 | + } |
| 1282 | - closeLive(); | 1285 | + return want; |
| 1283 | - liveUrl = url; | 1286 | + } |
| 1284 | - liveStream = new EventSource(url); | 1287 | + |
| 1285 | - liveStream.addEventListener('changed', function (e) { | 1288 | + function openLive(url) { |
| 1289 | + var es = new EventSource(url); | ||
| 1290 | + es.addEventListener('changed', function (e) { | ||
| 1286 | // A job page's stream opens with the status the page already shows: nothing to re-render then. | 1291 | // 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]'); | 1292 | + var job = document.querySelector('[data-live-log]'); |
| 1288 | - if (document.querySelector('[data-live-log]') && wrap && wrap.getAttribute('data-live-state') === e.data) { return; } | 1293 | + var wrap = job ? job.closest('[data-live]') : null; |
| 1294 | + if (wrap && wrap.getAttribute('data-live-state') === e.data) { return; } | ||
| 1289 | // A code view or an issue page re-renders at once (data-live-now). Events that come while a fetch | 1295 | // A code view or an issue page re-renders at once (data-live-now). Events that come while a fetch |
| 1290 | // is on the wire share the next one. | 1296 | // is on the wire share the next one. |
| 1291 | if (document.querySelector('[data-live-code], [data-live-now]')) { refreshLive(); return; } | 1297 | if (document.querySelector('[data-live-code], [data-live-now]')) { refreshLive(); return; } |
| 1292 | // The run list changes on every log chunk of every job: refresh at most once a second. | 1298 | // 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; } |
| 1294 | if (liveTimer) { return; } | 1300 | if (liveTimer) { return; } |
| 1295 | liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); | 1301 | liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); |
| 1296 | }); | 1302 | }); |
| 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; } | ||
| 1298 | } | 1324 | } |
| 1299 | 1325 | ||
| 1300 | // Appends streamed log text to the tail as a text node, never as HTML. The tail follows the end | 1326 | // Appends streamed log text to the tail as a text node, never as HTML. The tail follows the end |
| @@ -1311,7 +1337,7 @@ | |||
| 1311 | // A change re-renders the current page. It waits while a navigation is on the wire, since that | 1337 | // A change re-renders the current page. It waits while a navigation is on the wire, since that |
| 1312 | // response could be superseded: the change is kept and sent after that request completes. | 1338 | // response could be superseded: the change is kept and sent after that request completes. |
| 1313 | function refreshLive() { | 1339 | function refreshLive() { |
| 1314 | - if (!liveStream) { return; } | 1340 | + if (!Object.keys(liveStreams).length) { return; } |
| 1315 | if (userBusy > 0 || liveBusy) { liveDirty = true; return; } | 1341 | if (userBusy > 0 || liveBusy) { liveDirty = true; return; } |
| 1316 | liveDirty = false; | 1342 | liveDirty = false; |
| 1317 | liveBusy = true; | 1343 | liveBusy = true; |