pub struct Hotwatch { /* private fields */ }
Expand description
A non-blocking hotwatch instance.
Watching begins as soon as Self::watch
is called, and occurs in a
background thread. The background thread runs until this is dropped.
Dropping this will also unwatch everything.
Implementations§
source§impl Hotwatch
impl Hotwatch
sourcepub fn new_with_custom_delay(delay: Duration) -> Result<Self, Error>
pub fn new_with_custom_delay(delay: Duration) -> Result<Self, Error>
Using Hotwatch::new
will give you a default delay of 2 seconds.
This method allows you to specify your own value.
Notes
A delay of over 30 seconds will prevent repetitions of previous events on macOS.
sourcepub fn watch<P, F>(&mut self, path: P, handler: F) -> Result<(), Error>where
P: AsRef<Path>,
F: 'static + FnMut(Event) + Send,
pub fn watch<P, F>(&mut self, path: P, handler: F) -> Result<(), Error>where P: AsRef<Path>, F: 'static + FnMut(Event) + Send,
Watch a path and register a handler to it.
When watching a directory, that handler will receive all events for all directory contents, even recursing through subdirectories.
Only the most specific applicable handler will be called. In other words, if you’re
watching “dir” and “dir/file1”, then only the latter handler will fire for changes to
file1
.
Note that handlers will be run in hotwatch’s watch thread, so you’ll have to use move
if the closure captures anything.
Errors
Watching will fail if the path can’t be read, returning Error::Io
.
Examples
use hotwatch::{Hotwatch, Event, EventKind};
let mut hotwatch = Hotwatch::new().expect("hotwatch failed to initialize!");
hotwatch.watch("README.md", |event: Event| {
if let EventKind::Modify(_) = event.kind {
println!("{:?} changed!", event.paths[0]);
}
}).expect("failed to watch file!");