Commit b960e981 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Use a single debouncer, rather than two

parent d90e7b51
......@@ -80,33 +80,30 @@ fn notify_ipfs(add_to_ipfs: bool) {
// This does the proper debouncing (on top of the inotify debouncer) to emit the necessary
// messages to the console when we are expecting changes/are done with changes.
// If the variable wasn't updated, there's nothing for us to do.
if DEBOUNCING_DONE.compare_and_swap(!add_to_ipfs, add_to_ipfs, Ordering::Relaxed) == add_to_ipfs {
return;
}
if add_to_ipfs {
add_files_to_ipfs();
println!("Finished adding files to IPFS.\n");
} else {
DEBOUNCING_DONE.store(true, Ordering::Relaxed);
} else if DEBOUNCING_DONE.load(Ordering::Relaxed) == true {
println!("Change detected in filesystem.");
DEBOUNCING_DONE.store(false, Ordering::Relaxed);
}
}
fn files_changed(event: notify::DebouncedEvent, add_to_ipfs: bool) {
fn files_changed(event: notify::DebouncedEvent) {
// This function gets triggered when inotify notices files having changed.
//
// add_to_ipfs will be true when we actually need to add the files to IPFS (ie when enough time
// has passed after debouncing).
match event {
notify::DebouncedEvent::Create(_) => {}
notify::DebouncedEvent::Remove(_) => {}
notify::DebouncedEvent::Rename(_, _) => {}
notify::DebouncedEvent::Write(_) => {}
notify::DebouncedEvent::NoticeWrite(_) => notify_ipfs(false),
notify::DebouncedEvent::NoticeRemove(_) => notify_ipfs(false),
notify::DebouncedEvent::Create(_) => notify_ipfs(true),
notify::DebouncedEvent::Remove(_) => notify_ipfs(true),
notify::DebouncedEvent::Rename(_, _) => notify_ipfs(true),
notify::DebouncedEvent::Write(_) => notify_ipfs(true),
_ => return,
}
notify_ipfs(add_to_ipfs);
}
fn start_ipfs() {
......@@ -155,22 +152,7 @@ fn create_debounced_watcher() {
loop {
match rx.recv() {
Ok(e) => files_changed(e, true),
Err(e) => println!("watch error: {:?}", e),
}
}
}
fn create_immediate_watcher() {
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(0)).unwrap();
watcher
.watch(&*HEARTH_DIR, RecursiveMode::Recursive)
.expect("There was an error when trying to watch the Hearth directory for changes.");
loop {
match rx.recv() {
Ok(e) => files_changed(e, false),
Ok(e) => files_changed(e),
Err(e) => println!("watch error: {:?}", e),
}
}
......@@ -190,6 +172,5 @@ fn main() {
_ => {}
}
thread::spawn(create_immediate_watcher);
create_debounced_watcher();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment