Update to notify 3.0

notify now does its own event debouncing, so it's no longer necessary
for mdbook to do this manually.
This commit is contained in:
Matt Brubeck 2017-01-01 15:59:22 -08:00
parent 3ffd24df63
commit 1ac2602360
2 changed files with 54 additions and 58 deletions

View File

@ -26,7 +26,7 @@ toml = { version = "0.2", features = ["serde"] }
open = "1.1" open = "1.1"
# Watch feature # Watch feature
notify = { version = "2.5.5", optional = true } notify = { version = "3.0", optional = true }
time = { version = "0.1.34", optional = true } time = { version = "0.1.34", optional = true }
crossbeam = { version = "0.2.8", optional = true } crossbeam = { version = "0.2.8", optional = true }

View File

@ -35,6 +35,8 @@ use clap::{App, ArgMatches, SubCommand, AppSettings};
#[cfg(feature = "watch")] #[cfg(feature = "watch")]
use notify::Watcher; use notify::Watcher;
#[cfg(feature = "watch")] #[cfg(feature = "watch")]
use std::time::Duration;
#[cfg(feature = "watch")]
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
@ -187,15 +189,13 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
open(book.get_dest().join("index.html")); open(book.get_dest().join("index.html"));
} }
trigger_on_change(&mut book, |event, book| { trigger_on_change(&mut book, |path, book| {
if let Some(path) = event.path {
println!("File changed: {:?}\nBuilding book...\n", path); println!("File changed: {:?}\nBuilding book...\n", path);
match book.build() { match book.build() {
Err(e) => println!("Error while building: {:?}", e), Err(e) => println!("Error while building: {:?}", e),
_ => {}, _ => {},
} }
println!(""); println!("");
}
}); });
Ok(()) Ok(())
@ -258,15 +258,13 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
open(format!("http://{}", address)); open(format!("http://{}", address));
} }
trigger_on_change(&mut book, move |event, book| { trigger_on_change(&mut book, move |path, book| {
if let Some(path) = event.path {
println!("File changed: {:?}\nBuilding book...\n", path); println!("File changed: {:?}\nBuilding book...\n", path);
match book.build() { match book.build() {
Err(e) => println!("Error while building: {:?}", e), Err(e) => println!("Error while building: {:?}", e),
_ => broadcaster.send(RELOAD_COMMAND).unwrap(), _ => broadcaster.send(RELOAD_COMMAND).unwrap(),
} }
println!(""); println!("");
}
}); });
Ok(()) Ok(())
@ -307,53 +305,51 @@ fn open<P: AsRef<OsStr>>(path: P) {
// Calls the closure when a book source file is changed. This is blocking! // Calls the closure when a book source file is changed. This is blocking!
#[cfg(feature = "watch")] #[cfg(feature = "watch")]
fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> () fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
where F: Fn(notify::Event, &mut MDBook) -> () where F: Fn(&Path, &mut MDBook) -> ()
{ {
use notify::RecursiveMode::*;
use notify::DebouncedEvent::*;
// Create a channel to receive the events. // Create a channel to receive the events.
let (tx, rx) = channel(); let (tx, rx) = channel();
let w: Result<notify::RecommendedWatcher, notify::Error> = notify::Watcher::new(tx); let mut watcher = match notify::watcher(tx, Duration::from_secs(1)) {
Ok(w) => w,
Err(e) => {
println!("Error while trying to watch the files:\n\n\t{:?}", e);
::std::process::exit(0);
}
};
match w {
Ok(mut watcher) => {
// Add the source directory to the watcher // Add the source directory to the watcher
if let Err(e) = watcher.watch(book.get_src()) { if let Err(e) = watcher.watch(book.get_src(), Recursive) {
println!("Error while watching {:?}:\n {:?}", book.get_src(), e); println!("Error while watching {:?}:\n {:?}", book.get_src(), e);
::std::process::exit(0); ::std::process::exit(0);
}; };
// Add the book.json file to the watcher if it exists, because it's not // Add the book.{json,toml} file to the watcher if it exists, because it's not
// located in the source directory // located in the source directory
if let Err(_) = watcher.watch(book.get_root().join("book.json")) { if let Err(_) = watcher.watch(book.get_root().join("book.json"), NonRecursive) {
// do nothing if book.json is not found // do nothing if book.json is not found
} }
let mut previous_time = time::get_time();
println!("\nListening for changes...\n"); println!("\nListening for changes...\n");
loop { loop {
match rx.recv() { match rx.recv() {
Ok(event) => { Ok(event) => match event {
// Skip the event if an event has already been issued in the last second NoticeWrite(path) |
let time = time::get_time(); NoticeRemove(path) |
if time - previous_time < time::Duration::seconds(1) { Create(path) |
continue; Write(path) |
} else { Remove(path) |
previous_time = time; Rename(_, path) => {
closure(&path, book);
} }
_ => {}
closure(event, book);
}, },
Err(e) => { Err(e) => {
println!("An error occured: {:?}", e); println!("An error occured: {:?}", e);
}, },
} }
} }
},
Err(e) => {
println!("Error while trying to watch the files:\n\n\t{:?}", e);
::std::process::exit(0);
},
}
} }