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    folder: Arc<Folder>,
38    designs: Designs,
39    modules: Modules,
40    /// The comments the server writes through, which know the thread files
41    /// they wrote — and announced — themselves; `None` with comments off.
42    comments: Option<Comments>,
43    changes: Arc<dyn Publish>,
44    /// The search index, when this devpage answers searches.
45    search: Option<Arc<Search>>,
46    /// The slide PNGs kept, when this devpage draws them.
47    slide_pngs: Option<Arc<dyn SlidePngs>>,
48}
49
50impl Listeners {
51    /// The listeners of `folder`: each is the same value the server answers
52    /// from, so what it forgets the server reads again.
53    pub fn new(
54        folder: Arc<Folder>,
55        designs: Designs,
56        modules: Modules,
57        comments: Option<Comments>,
58        changes: Arc<dyn Publish>,
59        search: Option<Arc<Search>>,
60    ) -> Self {
61        Self {
62            folder,
63            designs,
64            modules,
65            comments,
66            changes,
67            search,
68            slide_pngs: None,
69        }
70    }
71
72    /// With the slide PNGs `slide_pngs` told of every change, before open
73    /// pages hear of it, so a page that asks for a slide's PNG again gets it
74    /// drawn as it is now.
75    pub fn with_slide_pngs(self, slide_pngs: Arc<dyn SlidePngs>) -> Self {
76        Self {
77            slide_pngs: Some(slide_pngs),
78            ..self
79        }
80    }
81
82    /// The served folder, which says what can matter and what to watch.
83    pub fn folder(&self) -> &Folder {
84        &self.folder
85    }
86
87    /// `paths` — canonical, or as reported when gone — changed, appeared or
88    /// vanished. The folder follows them; the design store and the widget
89    /// modules forget every one in the served folder and outside
90    /// `.devpage/`, whatever `watch` says, since any file may be part of a
91    /// design, and the slide PNGs hear of the same ones; open pages hear a
92    /// page's comments for each thread another hand changed — never the
93    /// server's own write, announced as it landed, and none with comments
94    /// off — then what the folder says; then the search index reads them
95    /// again.
96    /// Says whether the folders to watch may differ now.
97    pub fn changed(&self, paths: &[PathBuf]) -> bool {
98        let mut heard = self
99            .comments
100            .as_ref()
101            .map(|comments| comments.heard(paths))
102            .unwrap_or_default();
103        let folder = self.folder.changed(paths);
104        let files: Vec<PathBuf> = paths
105            .iter()
106            .filter(|path| {
107                self.folder
108                    .served()
109                    .relative(path)
110                    .is_some_and(|relative| !relative.starts_with(".devpage"))
111            })
112            .cloned()
113            .collect();
114        for file in &files {
115            self.designs.changed(file);
116            self.modules.forget(file);
117        }
118        if let Some(slide_pngs) = &self.slide_pngs
119            && !files.is_empty()
120        {
121            slide_pngs.changed(&files);
122        }
123        heard.extend(folder.changes);
124        for change in heard {
125            self.changes.publish(change);
126        }
127        if let Some(search) = &self.search {
128            search.changed(&self.folder, &files);
129        }
130        folder.reshaped
131    }
132}