thisweek_core/notify.rs
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
use core::time::Duration;
use notify_debouncer_mini::{new_debouncer, notify::*, DebounceEventResult};
use std::path::Path;
type Callback = fn();
pub struct Notify {
pub debouncer: notify_debouncer_mini::Debouncer<RecommendedWatcher>,
}
impl Notify {
pub fn new(path: &Path, callback: Callback) -> Self {
// Select recommended watcher for debouncer.
// Using a callback here, could also be a channel.
let mut debouncer = new_debouncer(
Duration::from_millis(500),
move |res: DebounceEventResult| match res {
Ok(events) => {
events
.iter()
.for_each(|e| println!("Event {:?} for {:?}", e.kind, e.path));
(callback)();
}
Err(e) => println!("Error {:?}", e),
},
)
.unwrap();
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
// println!("watching {path}");
debouncer
.watcher()
.watch(path, RecursiveMode::Recursive)
.unwrap();
Notify { debouncer }
}
// note that dropping the debouncer (as will happen here) also ends the debouncer
// thus this demo would need an endless loop to keep running
}