ankurah_signals/observer.rs
1use crate::Signal;
2mod callback_observer;
3pub use callback_observer::*;
4
5// Conditional bounds for Observer trait
6// When `multithread` is enabled, observers must be Send + Sync for cross-thread tracking
7#[cfg(feature = "multithread")]
8pub trait ObserverBounds: Send + Sync {}
9#[cfg(feature = "multithread")]
10impl<T: Send + Sync> ObserverBounds for T {}
11
12#[cfg(not(feature = "multithread"))]
13pub trait ObserverBounds {}
14#[cfg(not(feature = "multithread"))]
15impl<T> ObserverBounds for T {}
16
17/// An Observer is a struct that can observe multiple signals
18///
19/// When the `multithread` feature is enabled, observers must be `Send + Sync`
20/// to support cross-thread tracking (required for UniFFI/React Native).
21pub trait Observer: ObserverBounds {
22 /// Observe a signal reader - implement this method to handle subscriptions
23 fn observe(&self, signal: &dyn Signal);
24
25 /// Get a unique identifier for this observer (for equality comparison)
26 fn observer_id(&self) -> usize;
27
28 /// Downcast support for trait objects (used internally for diagnostics)
29 #[doc(hidden)]
30 fn as_any(&self) -> &dyn std::any::Any;
31}