2020-05-17 03:18:30 +00:00
|
|
|
use crossbeam_channel::Receiver;
|
|
|
|
use notify::{Event, RecommendedWatcher, RecursiveMode, Result, Watcher};
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-03-11 00:27:30 +00:00
|
|
|
/// Watches for changes to assets on the filesystem. This is used by the `AssetServer` to reload
|
|
|
|
/// them
|
2020-05-17 03:18:30 +00:00
|
|
|
pub struct FilesystemWatcher {
|
|
|
|
pub watcher: RecommendedWatcher,
|
|
|
|
pub receiver: Receiver<Result<Event>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FilesystemWatcher {
|
|
|
|
fn default() -> Self {
|
|
|
|
let (sender, receiver) = crossbeam_channel::unbounded();
|
|
|
|
let watcher: RecommendedWatcher = Watcher::new_immediate(move |res| {
|
2020-12-02 19:31:16 +00:00
|
|
|
sender.send(res).expect("Watch event send failure.");
|
2020-05-17 03:18:30 +00:00
|
|
|
})
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("Failed to create filesystem watcher.");
|
2020-05-17 03:18:30 +00:00
|
|
|
FilesystemWatcher { watcher, receiver }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FilesystemWatcher {
|
2020-05-29 19:56:32 +00:00
|
|
|
pub fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
|
2020-05-17 03:18:30 +00:00
|
|
|
self.watcher.watch(path, RecursiveMode::Recursive)
|
|
|
|
}
|
|
|
|
}
|