2019-01-26 03:15:27 +01:00
|
|
|
use notify::{raw_watcher, PollWatcher, RecommendedWatcher, RecursiveMode};
|
2017-02-04 22:18:02 +01:00
|
|
|
use std::path::PathBuf;
|
2016-10-20 20:27:11 +02:00
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
|
|
|
/// Thin wrapper over the notify crate
|
|
|
|
///
|
|
|
|
/// `PollWatcher` and `RecommendedWatcher` are distinct types, but watchexec
|
|
|
|
/// really just wants to handle them without regard to the exact type
|
|
|
|
/// (e.g. polymorphically). This has the nice side effect of separating out
|
|
|
|
/// all coupling to the notify crate into this module.
|
|
|
|
pub struct Watcher {
|
2016-10-24 02:12:48 +02:00
|
|
|
watcher_impl: WatcherImpl,
|
2016-10-20 20:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub use notify::Error;
|
2018-09-08 10:08:36 +02:00
|
|
|
pub use notify::RawEvent as Event;
|
2016-10-20 20:27:11 +02:00
|
|
|
|
|
|
|
enum WatcherImpl {
|
|
|
|
Recommended(RecommendedWatcher),
|
2016-10-24 02:12:48 +02:00
|
|
|
Poll(PollWatcher),
|
2016-10-20 20:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Watcher {
|
2018-09-08 10:08:36 +02:00
|
|
|
pub fn new(
|
|
|
|
tx: Sender<Event>,
|
|
|
|
paths: &[PathBuf],
|
|
|
|
poll: bool,
|
|
|
|
interval_ms: u32,
|
|
|
|
) -> Result<Watcher, Error> {
|
2017-02-04 22:18:02 +01:00
|
|
|
use notify::Watcher;
|
|
|
|
|
2016-10-20 20:27:11 +02:00
|
|
|
let imp = if poll {
|
2018-09-08 13:51:44 +02:00
|
|
|
let mut watcher = PollWatcher::with_delay_ms(tx, interval_ms)?;
|
2017-02-04 22:26:59 +01:00
|
|
|
for path in paths {
|
2018-09-08 13:51:44 +02:00
|
|
|
watcher.watch(path, RecursiveMode::Recursive)?;
|
2017-02-04 22:18:02 +01:00
|
|
|
debug!("Watching {:?}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
WatcherImpl::Poll(watcher)
|
2016-10-20 20:27:11 +02:00
|
|
|
} else {
|
2018-09-08 13:51:44 +02:00
|
|
|
let mut watcher = raw_watcher(tx)?;
|
2017-02-04 22:26:59 +01:00
|
|
|
for path in paths {
|
2018-09-08 13:51:44 +02:00
|
|
|
watcher.watch(path, RecursiveMode::Recursive)?;
|
2017-02-04 22:18:02 +01:00
|
|
|
debug!("Watching {:?}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
WatcherImpl::Recommended(watcher)
|
2016-10-20 20:27:11 +02:00
|
|
|
};
|
|
|
|
|
2017-02-04 22:18:02 +01:00
|
|
|
Ok(self::Watcher { watcher_impl: imp })
|
2016-10-20 20:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_polling(&self) -> bool {
|
|
|
|
if let WatcherImpl::Poll(_) = self.watcher_impl {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|