Skip to content

Commit 3a71371

Browse files
authored
Merge pull request #198 from mbrubeck/watch
Update watch command to use `notify` 3.0
2 parents 9a318ad + c7b4147 commit 3a71371

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ toml = { version = "0.2", features = ["serde"] }
2626
open = "1.1"
2727

2828
# Watch feature
29-
notify = { version = "2.5.5", optional = true }
29+
notify = { version = "3.0", optional = true }
3030
time = { version = "0.1.34", optional = true }
3131
crossbeam = { version = "0.2.8", optional = true }
3232

src/bin/mdbook.rs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ use clap::{App, ArgMatches, SubCommand, AppSettings};
3535
#[cfg(feature = "watch")]
3636
use notify::Watcher;
3737
#[cfg(feature = "watch")]
38+
use std::time::Duration;
39+
#[cfg(feature = "watch")]
3840
use std::sync::mpsc::channel;
3941

4042

@@ -187,15 +189,13 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
187189
open(book.get_dest().join("index.html"));
188190
}
189191

190-
trigger_on_change(&mut book, |event, book| {
191-
if let Some(path) = event.path {
192-
println!("File changed: {:?}\nBuilding book...\n", path);
193-
match book.build() {
194-
Err(e) => println!("Error while building: {:?}", e),
195-
_ => {},
196-
}
197-
println!("");
192+
trigger_on_change(&mut book, |path, book| {
193+
println!("File changed: {:?}\nBuilding book...\n", path);
194+
match book.build() {
195+
Err(e) => println!("Error while building: {:?}", e),
196+
_ => {},
198197
}
198+
println!("");
199199
});
200200

201201
Ok(())
@@ -258,15 +258,13 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
258258
open(format!("http://{}", address));
259259
}
260260

261-
trigger_on_change(&mut book, move |event, book| {
262-
if let Some(path) = event.path {
263-
println!("File changed: {:?}\nBuilding book...\n", path);
264-
match book.build() {
265-
Err(e) => println!("Error while building: {:?}", e),
266-
_ => broadcaster.send(RELOAD_COMMAND).unwrap(),
267-
}
268-
println!("");
261+
trigger_on_change(&mut book, move |path, book| {
262+
println!("File changed: {:?}\nBuilding book...\n", path);
263+
match book.build() {
264+
Err(e) => println!("Error while building: {:?}", e),
265+
_ => broadcaster.send(RELOAD_COMMAND).unwrap(),
269266
}
267+
println!("");
270268
});
271269

272270
Ok(())
@@ -307,53 +305,55 @@ fn open<P: AsRef<OsStr>>(path: P) {
307305
// Calls the closure when a book source file is changed. This is blocking!
308306
#[cfg(feature = "watch")]
309307
fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
310-
where F: Fn(notify::Event, &mut MDBook) -> ()
308+
where F: Fn(&Path, &mut MDBook) -> ()
311309
{
310+
use notify::RecursiveMode::*;
311+
use notify::DebouncedEvent::*;
312+
312313
// Create a channel to receive the events.
313314
let (tx, rx) = channel();
314315

315-
let w: Result<notify::RecommendedWatcher, notify::Error> = notify::Watcher::new(tx);
316+
let mut watcher = match notify::watcher(tx, Duration::from_secs(1)) {
317+
Ok(w) => w,
318+
Err(e) => {
319+
println!("Error while trying to watch the files:\n\n\t{:?}", e);
320+
::std::process::exit(0);
321+
}
322+
};
316323

317-
match w {
318-
Ok(mut watcher) => {
319-
// Add the source directory to the watcher
320-
if let Err(e) = watcher.watch(book.get_src()) {
321-
println!("Error while watching {:?}:\n {:?}", book.get_src(), e);
322-
::std::process::exit(0);
323-
};
324+
// Add the source directory to the watcher
325+
if let Err(e) = watcher.watch(book.get_src(), Recursive) {
326+
println!("Error while watching {:?}:\n {:?}", book.get_src(), e);
327+
::std::process::exit(0);
328+
};
324329

325-
// Add the book.json file to the watcher if it exists, because it's not
326-
// located in the source directory
327-
if let Err(_) = watcher.watch(book.get_root().join("book.json")) {
328-
// do nothing if book.json is not found
329-
}
330+
// Add the book.{json,toml} file to the watcher if it exists, because it's not
331+
// located in the source directory
332+
if let Err(_) = watcher.watch(book.get_root().join("book.json"), NonRecursive) {
333+
// do nothing if book.json is not found
334+
}
335+
if let Err(_) = watcher.watch(book.get_root().join("book.toml"), NonRecursive) {
336+
// do nothing if book.toml is not found
337+
}
330338

331-
let mut previous_time = time::get_time();
332-
333-
println!("\nListening for changes...\n");
334-
335-
loop {
336-
match rx.recv() {
337-
Ok(event) => {
338-
// Skip the event if an event has already been issued in the last second
339-
let time = time::get_time();
340-
if time - previous_time < time::Duration::seconds(1) {
341-
continue;
342-
} else {
343-
previous_time = time;
344-
}
345-
346-
closure(event, book);
347-
},
348-
Err(e) => {
349-
println!("An error occured: {:?}", e);
350-
},
339+
println!("\nListening for changes...\n");
340+
341+
loop {
342+
match rx.recv() {
343+
Ok(event) => match event {
344+
NoticeWrite(path) |
345+
NoticeRemove(path) |
346+
Create(path) |
347+
Write(path) |
348+
Remove(path) |
349+
Rename(_, path) => {
350+
closure(&path, book);
351351
}
352-
}
353-
},
354-
Err(e) => {
355-
println!("Error while trying to watch the files:\n\n\t{:?}", e);
356-
::std::process::exit(0);
357-
},
352+
_ => {}
353+
},
354+
Err(e) => {
355+
println!("An error occured: {:?}", e);
356+
},
357+
}
358358
}
359359
}

0 commit comments

Comments
 (0)