grafos_registry/
watcher.rs

1//! Version-based change detection for the registry.
2
3/// Tracks the last-seen registry version and detects changes.
4///
5/// Create a watcher from [`RegistryReader::watch`](crate::RegistryReader::watch)
6/// or directly with [`RegistryWatcher::new`]. Call [`changed`](Self::changed)
7/// with the current registry version to check for updates.
8pub struct RegistryWatcher {
9    last_seen_version: u64,
10}
11
12impl RegistryWatcher {
13    /// Create a watcher starting at the given version.
14    pub fn new(initial_version: u64) -> Self {
15        RegistryWatcher {
16            last_seen_version: initial_version,
17        }
18    }
19
20    /// Check whether the registry has changed since the last acknowledgement.
21    pub fn changed(&self, current_version: u64) -> bool {
22        current_version > self.last_seen_version
23    }
24
25    /// Acknowledge the current version, resetting the change flag.
26    pub fn acknowledge(&mut self, current_version: u64) {
27        self.last_seen_version = current_version;
28    }
29
30    /// Return the last acknowledged version.
31    pub fn last_seen_version(&self) -> u64 {
32        self.last_seen_version
33    }
34}