Skip to main content

infigraph_core/watch/
batch.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3use std::time::{Duration, Instant};
4
5pub struct ChangeBatch {
6    paths: HashSet<PathBuf>,
7    window: Duration,
8    last_event: Option<Instant>,
9}
10
11impl ChangeBatch {
12    pub fn new(window_ms: u64) -> Self {
13        Self {
14            paths: HashSet::new(),
15            window: Duration::from_millis(window_ms),
16            last_event: None,
17        }
18    }
19
20    /// Add a path to the current batch.
21    pub fn add(&mut self, path: PathBuf) {
22        self.paths.insert(path);
23        self.last_event = Some(Instant::now());
24    }
25
26    /// Check if the batch window has closed (enough time since last event).
27    pub fn is_ready(&self) -> bool {
28        match self.last_event {
29            Some(t) => t.elapsed() >= self.window,
30            None => false,
31        }
32    }
33
34    /// Drain the batch and return accumulated paths.
35    pub fn drain(&mut self) -> Vec<PathBuf> {
36        self.last_event = None;
37        self.paths.drain().collect()
38    }
39
40    /// Whether the batch has any pending paths.
41    pub fn is_empty(&self) -> bool {
42        self.paths.is_empty()
43    }
44
45    pub fn len(&self) -> usize {
46        self.paths.len()
47    }
48}