Skip to main content

devpage_core/watch/
mod.rs

1//! Local mode's one ordered response to saved files. The binary's watcher
2//! turns the platform's file events into bursts of changed paths and hands
3//! each burst here; everything that follows from it is decided here, in one
4//! order: the folder follows the paths, the design store and the widget
5//! modules forget what they read of them, the slide PNGs kept forget each
6//! slide whose deck a path is part of, open pages hear what changed —
7//! a page's comments for each thread another hand changed, then at most one
8//! refresh — and last the search index reads them again, so indexing text
9//! never holds up a refresh. Server mode has none of this: a published
10//! version never changes.
11
12#[cfg(test)]
13mod tests;
14
15use std::path::PathBuf;
16use std::sync::Arc;
17
18use crate::changes::Publish;
19use crate::comments::Comments;
20use crate::designs::Designs;
21use crate::folder::Folder;
22use crate::search::Search;
23use crate::widgets::Modules;
24
25/// The slide PNGs a server keeps — the binary's, drawn by Chrome — told of
26/// the files that changed so each forgets the slides whose deck a change
27/// reaches ([`crate::check::drawn_from`]) and no other.
28pub trait SlidePngs: Send + Sync {
29    /// `files` — canonical, or as reported when gone — changed, appeared or
30    /// vanished.
31    fn changed(&self, files: &[PathBuf]);
32}
33
34/// Who hears about the files that change in the served folder, and the bus
35/// open pages hear it on.
36pub struct Listeners {
37    /// The served folder, which follows the paths first.
38    folder: Arc<Folder>,
39    /// The design store, which forgets what it read of each changed file.
40    designs: Designs,
41    /// The widget modules, which forget each changed file they compiled.
42    modules: Modules,
43    /// The comments the server writes through, which know the thread files
44    /// they wrote — and announced — themselves; `None` with comments off.
45    comments: Option<Comments>,
46    /// The bus open pages hear what changed on.
47    changes: Arc<dyn Publish>,
48    /// The search index, when this devpage answers searches.
49    search: Option<Arc<Search>>,
50    /// The slide PNGs kept, when this devpage draws them.
51    slide_pngs: Option<Arc<dyn SlidePngs>>,
52}
53
54impl Listeners {
55    /// The listeners of `folder`: each is the same value the server answers
56    /// from, so what it forgets the server reads again.
57    pub fn new(
58        folder: Arc<Folder>,
59        designs: Designs,
60        modules: Modules,
61        comments: Option<Comments>,
62        changes: Arc<dyn Publish>,
63        search: Option<Arc<Search>>,
64    ) -> Self {
65        Self {
66            folder,
67            designs,
68            modules,
69            comments,
70            changes,
71            search,
72            slide_pngs: None,
73        }
74    }
75
76    /// With the slide PNGs `slide_pngs` told of every change, before open
77    /// pages hear of it, so a page that asks for a slide's PNG again gets it
78    /// drawn as it is now.
79    pub fn with_slide_pngs(self, slide_pngs: Arc<dyn SlidePngs>) -> Self {
80        Self {
81            slide_pngs: Some(slide_pngs),
82            ..self
83        }
84    }
85
86    /// The served folder, which says what can matter and what to watch.
87    pub fn folder(&self) -> &Folder {
88        &self.folder
89    }
90
91    /// `paths` — canonical, or as reported when gone — changed, appeared or
92    /// vanished. The folder follows them; the design store and the widget
93    /// modules forget every one in the served folder and outside
94    /// `.devpage/`, whatever `watch` says, since any file may be part of a
95    /// design, and the slide PNGs hear of the same ones; open pages hear a
96    /// page's comments for each thread another hand changed — never the
97    /// server's own write, announced as it landed, and none with comments
98    /// off — then what the folder says; then the search index reads them
99    /// again.
100    /// Says whether the folders to watch may differ now.
101    pub fn changed(&self, paths: &[PathBuf]) -> bool {
102        let mut heard = self
103            .comments
104            .as_ref()
105            .map(|comments| comments.heard(paths))
106            .unwrap_or_default();
107        let folder = self.folder.changed(paths);
108        let files: Vec<PathBuf> = paths
109            .iter()
110            .filter(|path| {
111                self.folder
112                    .served()
113                    .relative(path)
114                    .is_some_and(|relative| !relative.starts_with(".devpage"))
115            })
116            .cloned()
117            .collect();
118        for file in &files {
119            self.designs.changed(file);
120            self.modules.forget(file);
121        }
122        if let Some(slide_pngs) = &self.slide_pngs
123            && !files.is_empty()
124        {
125            slide_pngs.changed(&files);
126        }
127        heard.extend(folder.changes);
128        let code = crate::code::changed(self.folder.served(), &files);
129        if (crate::extensions::changed(self.folder.served(), &files) || code)
130            && !heard.contains(&crate::changes::Change::Refresh)
131        {
132            heard.push(crate::changes::Change::Refresh);
133        }
134        for change in heard {
135            self.changes.publish(change);
136        }
137        if let Some(search) = &self.search {
138            search.changed(&self.folder, &files);
139        }
140        folder.reshaped
141    }
142}