Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::Result;
use crossbeam_channel::{unbounded, Sender};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_mini::{new_debouncer, DebounceEventResult};
use scopetime::scope_time;
use notify_debouncer_mini::{
new_debouncer, DebounceEventResult, Debouncer,
};
use std::{path::Path, thread, time::Duration};

pub struct RepoWatcher {
receiver: crossbeam_channel::Receiver<()>,
_watcher: Debouncer<RecommendedWatcher>,
}

impl RepoWatcher {
Expand All @@ -18,12 +20,13 @@ impl RepoWatcher {

let (tx, rx) = std::sync::mpsc::channel();

let workdir = workdir.to_string();

thread::spawn(move || {
let timeout = Duration::from_secs(2);
create_watcher(timeout, tx, &workdir);
});
let timeout = Duration::from_secs(2);
let mut bouncer =
new_debouncer(timeout, tx).expect("Watch create error");
bouncer
.watcher()
.watch(Path::new(workdir), RecursiveMode::Recursive)
.expect("Watch error");

let (out_tx, out_rx) = unbounded();

Expand All @@ -34,7 +37,10 @@ impl RepoWatcher {
}
});

Self { receiver: out_rx }
Self {
receiver: out_rx,
_watcher: bouncer,
}
}

///
Expand Down Expand Up @@ -63,20 +69,3 @@ impl RepoWatcher {
}
}
}

fn create_watcher(
timeout: Duration,
tx: std::sync::mpsc::Sender<DebounceEventResult>,
workdir: &str,
) {
scope_time!("create_watcher");

let mut bouncer =
new_debouncer(timeout, tx).expect("Watch create error");
bouncer
.watcher()
.watch(Path::new(&workdir), RecursiveMode::Recursive)
.expect("Watch error");

std::mem::forget(bouncer);
}