diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index e65035b..373ed16 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -990,8 +990,9 @@ // 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; } - // A code view re-renders at once. Events that come while a fetch is on the wire share the next one. - if (document.querySelector('[data-live-code]')) { refreshLive(); 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 (liveTimer) { return; } diff --git a/crates/forge-web/src/live.rs b/crates/forge-web/src/live.rs index 8ad5ea1..be336e7 100644 --- a/crates/forge-web/src/live.rs +++ b/crates/forge-web/src/live.rs @@ -14,6 +14,10 @@ //! one, so a bump that changes nothing visible sends nothing. //! 4. Render `data-live=""` on the page's element that the client follows. //! +//! A page whose state is cheap to compare can use `stream_changes` (the fingerprint). A page +//! whose every bump is a visible change (the issue pages) uses [`stream_wakes`] instead: one +//! `changed` per wake, several bumps in a row coalesced into one. +//! //! The hub is in-process only, like [`crate::notify_hub::NotifyHub`]. [`crate::ci::CiLive`] keeps //! its own counters for CI and is not on this hub. @@ -35,6 +39,11 @@ use crate::pages::ci_runs::events_response; pub enum Topic { /// The refs of a repo: a branch or tag was created, deleted or moved. Keyed by repo id. RepoRefs(String), + /// The issues of a repo as a list: an issue, label or milestone changed, or a count moved. + /// Keyed by repo id. The labels and milestones pages and the repo tabs follow it too. + RepoIssues(String), + /// One issue: its body, comments, events, state, labels, reactions. Keyed by issue id. + Issue(String), } /// The open topics, each with the counter its subscribers wait on. @@ -71,6 +80,19 @@ impl LiveHub { topics.remove(topic); } } + + /// Wakes the issue list and the labels, milestones and tabs that follow the repo's issues. Call + /// it after a change to a label or milestone of `repo_id`. + pub fn bump_repo_issues(&self, repo_id: &str) { + self.bump(&Topic::RepoIssues(repo_id.to_owned())); + } + + /// Wakes the issue list of `repo_id` and the page of `issue_id`. Call it after an issue write + /// has succeeded, with the repo the issue belongs to. + pub fn bump_issue(&self, repo_id: &str, issue_id: &str) { + self.bump(&Topic::RepoIssues(repo_id.to_owned())); + self.bump(&Topic::Issue(issue_id.to_owned())); + } } /// The state a stream follows between wakes: the fingerprint last sent or seen. @@ -85,6 +107,22 @@ fn changed_event() -> Result { Ok(Event::new().event("changed").data("1")) } +/// The response of a live stream for any of `topics`. It sends `changed` after each wake, with no +/// fingerprint: the caller bumps only after a write that changes the page. Nothing is sent on +/// connect. The stream ends when a topic is gone, which cannot happen while the stream holds it. +pub fn stream_wakes(cx: &Cx, hub: &LiveHub, topics: &[Topic]) -> Result { + let wakes: Vec> = topics.iter().map(|t| hub.subscribe(t)).collect(); + let events = stream::unfold(wakes, |mut wakes| async move { + // The futures of the topics not woken are dropped with this statement. + let (woken, _, _) = + futures_util::future::select_all(wakes.iter_mut().map(|wake| Box::pin(wake.changed()))) + .await; + woken.ok()?; + Some((changed_event(), wakes)) + }); + events_response(cx, events) +} + /// The response of a live stream for `topic`. It sends `changed` after each wake whose /// fingerprint differs from the last one. Nothing is sent on connect: the page already shows the /// state the first read saw. The stream ends if the read fails. diff --git a/crates/forge-web/src/pages/edit_history.rs b/crates/forge-web/src/pages/edit_history.rs index 9018590..64dc936 100644 --- a/crates/forge-web/src/pages/edit_history.rs +++ b/crates/forge-web/src/pages/edit_history.rs @@ -18,6 +18,7 @@ use topcoat::view::{View, component, view}; use crate::i18n::issues::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::issue_parts::{ItemKind, When, item_for, parse_number}; +use crate::pages::issues_live::item_live_url; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; use crate::state::AppState; @@ -226,11 +227,13 @@ async fn history_page( let heading = g::history_heading(lang, what); let back_label = g::back_to_item(lang, number); let count_label = g::earlier_versions(lang, entries.len()); + // An issue's history follows the issue: a new edit adds a version to the list. + let live = (kind_item == ItemKind::Issue).then(|| item_live_url(&owner, &name, number)); Ok(view! { cx => shell(title: page_title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) -
+

(heading.as_str())