Group file changes and rebuild book only once

This commit is contained in:
Cauê Baasch de Souza 2019-01-31 16:31:02 -02:00
parent c068703028
commit c7281459f9
2 changed files with 30 additions and 16 deletions

View File

@ -115,8 +115,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
} }
#[cfg(feature = "watch")] #[cfg(feature = "watch")]
watch::trigger_on_change(&book, move |path, book_dir| { watch::trigger_on_change(&book, move |paths, book_dir| {
info!("File changed: {:?}", path); info!("Files changed: {:?}", paths);
info!("Building book..."); info!("Building book...");
// FIXME: This area is really ugly because we need to re-set livereload :( // FIXME: This area is really ugly because we need to re-set livereload :(

View File

@ -5,9 +5,10 @@ use clap::{App, ArgMatches, SubCommand};
use mdbook::errors::Result; use mdbook::errors::Result;
use mdbook::utils; use mdbook::utils;
use mdbook::MDBook; use mdbook::MDBook;
use std::path::Path; use std::path::{Path, PathBuf};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::Duration; use std::time::Duration;
use std::thread::sleep;
use {get_book_dir, open}; use {get_book_dir, open};
// Create clap subcommand arguments // Create clap subcommand arguments
@ -18,10 +19,12 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\ "-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
Relative paths are interpreted relative to the book's root directory.{n}\ Relative paths are interpreted relative to the book's root directory.{n}\
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'", If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
).arg_from_usage( )
.arg_from_usage(
"[dir] 'Root directory for the book{n}\ "[dir] 'Root directory for the book{n}\
(Defaults to the Current Directory when omitted)'", (Defaults to the Current Directory when omitted)'",
).arg_from_usage("-o, --open 'Open the compiled book in a web browser'") )
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
} }
// Watch command implementation // Watch command implementation
@ -34,8 +37,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
open(book.build_dir_for("html").join("index.html")); open(book.build_dir_for("html").join("index.html"));
} }
trigger_on_change(&book, |path, book_dir| { trigger_on_change(&book, |paths, book_dir| {
info!("File changed: {:?}\nBuilding book...\n", path); info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(&book_dir).and_then(|b| b.build()); let result = MDBook::load(&book_dir).and_then(|b| b.build());
if let Err(e) = result { if let Err(e) = result {
@ -50,7 +53,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
/// Calls the closure when a book source file is changed, blocking indefinitely. /// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F) pub fn trigger_on_change<F>(book: &MDBook, closure: F)
where where
F: Fn(&Path, &Path), F: Fn(Vec<PathBuf>, &Path),
{ {
use self::notify::DebouncedEvent::*; use self::notify::DebouncedEvent::*;
use self::notify::RecursiveMode::*; use self::notify::RecursiveMode::*;
@ -79,13 +82,24 @@ where
info!("Listening for changes..."); info!("Listening for changes...");
for event in rx.iter() { loop {
let first_event = rx.recv().unwrap();
sleep(Duration::from_millis(50));
let other_events = rx.try_iter();
let all_events = std::iter::once(first_event).chain(other_events);
let paths = all_events
.filter_map(|event| {
debug!("Received filesystem event: {:?}", event); debug!("Received filesystem event: {:?}", event);
match event { match event {
Create(path) | Write(path) | Remove(path) | Rename(_, path) => { Create(path) | Write(path) | Remove(path) | Rename(_, path) => Some(path),
closure(&path, &book.root); _ => None,
}
_ => {}
} }
})
.collect();
closure(paths, &book.root);
} }
} }