revoke_config/
watcher.rs

1use crate::types::{ConfigChange, ConfigChangeCallback};
2use futures::Stream;
3use std::pin::Pin;
4use std::sync::Arc;
5use parking_lot::RwLock;
6use tracing::debug;
7
8pub struct ConfigWatcher {
9    callbacks: Arc<RwLock<Vec<ConfigChangeCallback>>>,
10}
11
12impl ConfigWatcher {
13    pub fn new() -> Self {
14        Self {
15            callbacks: Arc::new(RwLock::new(Vec::new())),
16        }
17    }
18
19    pub fn subscribe(&self, callback: ConfigChangeCallback) {
20        self.callbacks.write().push(callback);
21    }
22
23    pub fn notify(&self, change: ConfigChange) {
24        let callbacks = self.callbacks.read();
25        for callback in callbacks.iter() {
26            callback(change.clone());
27        }
28    }
29
30    pub async fn watch_stream(
31        &self,
32        mut stream: Pin<Box<dyn Stream<Item = ConfigChange> + Send>>,
33    ) {
34        use futures::StreamExt;
35        
36        while let Some(change) = stream.next().await {
37            debug!("Config change detected: {:?}", change);
38            self.notify(change);
39        }
40    }
41}
42
43impl Default for ConfigWatcher {
44    fn default() -> Self {
45        Self::new()
46    }
47}