Skip to main content

devpage_core/
changes.rs

1//! What open pages hear: a change is typed — refresh the page, or fetch a
2//! page's comments again — and scoped to the documents it concerns, so a
3//! page hears only its own. Anything may publish through the [`Publish`]
4//! port: the file watcher, a comment store, the external-link checker. The
5//! bus that carries changes to open pages is the `devpage` binary's.
6
7use std::sync::Arc;
8
9/// Which documents a change concerns: the documents served under one mount
10/// prefix — every document of the served folder at `/` — or every document
11/// there is.
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub enum Scope {
14    /// What every open page may show, whatever its document: an external
15    /// link's answer.
16    Every,
17    /// The documents served under this mount prefix; empty at `/`.
18    Mount(Arc<str>),
19}
20
21impl Scope {
22    /// The documents served at `/` — everything a local devpage serves.
23    pub fn root() -> Self {
24        Self::Mount(Arc::from(""))
25    }
26
27    /// Whether a page subscribed to `self` hears a change to `scope`.
28    pub fn hears(&self, scope: &Scope) -> bool {
29        matches!(scope, Scope::Every) || scope == self
30    }
31}
32
33/// Something new for open pages to show.
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub enum Change {
36    /// Fetch the page again.
37    Refresh,
38    /// The threads on the Markdown file at this address changed.
39    Comments(String),
40}
41
42impl Change {
43    /// The message the page script reads off its event stream.
44    pub fn message(&self) -> String {
45        match self {
46            Change::Refresh => "change".into(),
47            Change::Comments(page) => format!("comments {page}"),
48        }
49    }
50}
51
52/// Where a change goes: the bus open pages listen to.
53pub trait Publish: Send + Sync {
54    /// Tells every page subscribed to `scope` — or to any scope, for
55    /// [`Scope::Every`] — about `change`. No one listening is not an error.
56    fn publish(&self, scope: &Scope, change: Change);
57}
58
59/// A [`Publish`] that keeps what it was told, for tests.
60#[cfg(any(test, feature = "testing"))]
61#[derive(Default)]
62pub struct Recorder(std::sync::Mutex<Vec<(Scope, Change)>>);
63
64#[cfg(any(test, feature = "testing"))]
65impl Recorder {
66    /// The changes published to `scope` — or to every scope — so far, in
67    /// order, forgotten once taken.
68    pub fn drain(&self, scope: &Scope) -> Vec<Change> {
69        std::mem::take(&mut *self.0.lock().unwrap())
70            .into_iter()
71            .filter(|(published, _)| scope.hears(published))
72            .map(|(_, change)| change)
73            .collect()
74    }
75}
76
77#[cfg(any(test, feature = "testing"))]
78impl Publish for Recorder {
79    fn publish(&self, scope: &Scope, change: Change) {
80        self.0.lock().unwrap().push((scope.clone(), change));
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn the_page_reads_the_same_messages_as_before() {
90        assert_eq!(Change::Refresh.message(), "change");
91        assert_eq!(
92            Change::Comments("/docs/a.md".into()).message(),
93            "comments /docs/a.md"
94        );
95    }
96}