use std::path::PathBuf;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
const DEBOUNCE: Duration = Duration::from_millis(300);
#[derive(Debug, Default)]
pub struct Nudge {
pending: Mutex<bool>,
signal: Condvar,
}
impl Nudge {
pub fn wake(&self) {
let mut pending = self.pending.lock().expect("the nudge lock is poisoned");
*pending = true;
self.signal.notify_all();
}
pub fn wait(&self, timeout: Duration) {
let pending = self.pending.lock().expect("the nudge lock is poisoned");
if *pending {
drop(pending);
self.take();
return;
}
let (mut pending, _) = self
.signal
.wait_timeout(pending, timeout)
.expect("the nudge lock is poisoned");
*pending = false;
}
fn take(&self) {
let mut pending = self.pending.lock().expect("the nudge lock is poisoned");
*pending = false;
}
}
pub fn spawn(roots: Vec<PathBuf>, nudge: Arc<Nudge>) -> Result<usize, String> {
if roots.is_empty() {
return Ok(0);
}
let count = roots.len();
crate::web::fs_scope::spawn_scoped_watcher(
"peer-continuity".to_string(),
roots,
true,
DEBOUNCE,
move |events| {
if events.is_empty() {
return;
}
nudge.wake();
},
)?;
Ok(count)
}
#[cfg(test)]
#[path = "watch_tests.rs"]
mod tests;