1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Utility to detect changed files for shader hot-reloading.
use async_channel::Receiver;
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[cfg(doc)]
use crate::Shader;
/// State for tracking file changes.
pub struct HotReloadState {
#[cfg(not(target_family = "wasm"))]
watcher: RecommendedWatcher,
rcv: Receiver<notify::Result<Event>>,
file_changed: HashMap<PathBuf, bool>,
}
impl HotReloadState {
/// Initializes the file-tracking context.
///
/// To register a shader for change-tracking call [`Shader::watch_sources`] once with the state
/// returned by this function.
/// To register a file for change-tracking, call [`HotReloadState::watch_file`].
pub fn new() -> notify::Result<Self> {
let (snd, rcv) = async_channel::unbounded();
Ok(Self {
#[cfg(not(target_family = "wasm"))]
watcher: notify::recommended_watcher(move |msg| {
// TODO: does hot-reloading make sense on wasm anyway?
let _ = snd.send_blocking(msg);
})?,
rcv,
file_changed: Default::default(),
})
}
/// Saves in `self` the set of watched files that changed since the last time this function
/// was called.
///
/// Once this call completes, the [`Self::file_changed`] method can be used to check if a
/// particular file (assuming it was added to the watch list with [`Self::watch_file`]) has
/// changed since the last time [`Self::updated_changes`] was called.
pub fn update_changes(&mut self) {
for changed in self.file_changed.values_mut() {
*changed = false;
}
while let Ok(event) = self.rcv.try_recv() {
if let Ok(event) = event {
if event.need_rescan() || matches!(event.kind, EventKind::Modify(_)) {
for path in event.paths {
self.file_changed.insert(path, true);
}
}
}
}
}
/// Registers a files for change-tracking.
pub fn watch_file(&mut self, path: &Path) -> notify::Result<()> {
#[cfg(not(target_family = "wasm"))]
if !self.file_changed.contains_key(path) {
self.watcher.watch(path, RecursiveMode::NonRecursive)?;
// NOTE: this won’t insert if the watch failed.
self.file_changed.insert(path.to_path_buf(), false);
}
Ok(())
}
/// Checks if the specified file change was detected at the time of calling [`Self::update_changes`].
pub fn file_changed(&self, path: &Path) -> bool {
self.file_changed.get(path).copied().unwrap_or_default()
}
}