use std::collections::HashSet;
use std::path::PathBuf;
use std::time::{Duration, Instant};
pub struct ChangeBatch {
paths: HashSet<PathBuf>,
window: Duration,
last_event: Option<Instant>,
}
impl ChangeBatch {
pub fn new(window_ms: u64) -> Self {
Self {
paths: HashSet::new(),
window: Duration::from_millis(window_ms),
last_event: None,
}
}
pub fn add(&mut self, path: PathBuf) {
self.paths.insert(path);
self.last_event = Some(Instant::now());
}
pub fn is_ready(&self) -> bool {
match self.last_event {
Some(t) => t.elapsed() >= self.window,
None => false,
}
}
pub fn drain(&mut self) -> Vec<PathBuf> {
self.last_event = None;
self.paths.drain().collect()
}
pub fn is_empty(&self) -> bool {
self.paths.is_empty()
}
pub fn len(&self) -> usize {
self.paths.len()
}
}