bevy/crates/bevy_asset/src/filesystem_watcher.rs

28 lines
917 B
Rust
Raw Normal View History

2020-05-17 03:18:30 +00:00
use crossbeam_channel::Receiver;
use notify::{Event, RecommendedWatcher, RecursiveMode, Result, Watcher};
use std::path::Path;
/// 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 = RecommendedWatcher::new(move |res| {
sender.send(res).expect("Watch event send failure.");
2020-05-17 03:18:30 +00:00
})
.expect("Failed to create filesystem watcher.");
2020-05-17 03:18:30 +00:00
FilesystemWatcher { watcher, receiver }
}
}
impl FilesystemWatcher {
pub fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
self.watcher.watch(path.as_ref(), RecursiveMode::Recursive)
2020-05-17 03:18:30 +00:00
}
}