agtrace_sdk/
watch.rs

1use std::sync::Arc;
2
3use crate::error::Result;
4
5// Re-export event types for convenient use in examples/client code
6pub use agtrace_runtime::{DiscoveryEvent, StreamEvent, WorkspaceEvent};
7
8pub struct WatchBuilder {
9    inner: Arc<agtrace_runtime::AgTrace>,
10    providers: Vec<String>,
11}
12
13impl WatchBuilder {
14    pub(crate) fn new(inner: Arc<agtrace_runtime::AgTrace>) -> Self {
15        Self {
16            inner,
17            providers: vec![],
18        }
19    }
20
21    pub fn provider(mut self, name: &str) -> Self {
22        self.providers.push(name.to_string());
23        self
24    }
25
26    pub fn all_providers(mut self) -> Self {
27        self.providers.clear();
28        self
29    }
30
31    pub fn start(self) -> Result<LiveStream> {
32        let monitor = self
33            .inner
34            .workspace_monitor()
35            .map_err(crate::error::Error::Internal)?
36            .start_background_scan()
37            .map_err(crate::error::Error::Internal)?;
38        Ok(LiveStream { monitor })
39    }
40}
41
42pub struct LiveStream {
43    monitor: agtrace_runtime::WorkspaceMonitor,
44}
45
46impl LiveStream {
47    pub fn next_blocking(&self) -> Option<WorkspaceEvent> {
48        self.monitor.receiver().recv().ok()
49    }
50
51    pub fn try_next(&self) -> Option<WorkspaceEvent> {
52        self.monitor.receiver().try_recv().ok()
53    }
54}
55
56impl Iterator for LiveStream {
57    type Item = WorkspaceEvent;
58
59    fn next(&mut self) -> Option<Self::Item> {
60        self.next_blocking()
61    }
62}