pub struct Hotwatch { /* private fields */ }
Expand description
A blocking hotwatch instance.
No watching will actually happen until you call Hotwatch::run
, which blocks
the thread until a handler returns Flow::Exit
. This is useful if you just
want to wait on some criteria, rather than if you’re building some long-running
sexy hot reload service.
Dropping this will 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) -> Flow,
pub fn watch<P, F>(&mut self, path: P, handler: F) -> Result<(), Error>where P: AsRef<Path>, F: 'static + FnMut(Event) -> Flow,
Watch a path and register a handler to it.
Handlers won’t actually be run until you call Hotwatch::run
.
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
.
Errors
Watching will fail if the path can’t be read, returning Error::Io
.
Examples
use hotwatch::{blocking::{Flow, Hotwatch}, Event, EventKind};
let mut hotwatch = Hotwatch::new().expect("hotwatch failed to initialize!");
// Note that this won't actually do anything until you call `hotwatch.run()`!
hotwatch.watch("README.md", |event: Event| {
if let EventKind::Modify(_) = event.kind {
println!("{:?} changed!", event.paths[0]);
Flow::Exit
} else {
Flow::Continue
}
}).expect("failed to watch file!");
sourcepub fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error>
pub fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error>
Stop watching a path.
Errors
This will fail if the path wasn’t being watched, or if the path couldn’t be unwatched for some platform-specific internal reason.
sourcepub fn run(&mut self)
pub fn run(&mut self)
Run handlers in an endless loop, blocking the thread.
The loop will only exit if a handler returns Flow::Exit
.