fedimint_client/sm/
notifier.rs1use fedimint_client_module::module::FinalClientIface;
2use fedimint_client_module::sm::{DynState, ModuleNotifier};
3use fedimint_core::core::ModuleInstanceId;
4use fedimint_core::util::FmtCompact;
5use tracing::{debug, trace};
6
7#[derive(Clone)]
14pub struct Notifier {
15 broadcast: tokio::sync::broadcast::Sender<DynState>,
17}
18
19impl Notifier {
20 #[allow(clippy::new_without_default)]
21 pub fn new() -> Self {
22 let (sender, _receiver) = tokio::sync::broadcast::channel(10_000);
23 Self { broadcast: sender }
24 }
25
26 pub fn notify(&self, state: DynState) {
28 let queue_len = self.broadcast.len();
29 trace!(?state, %queue_len, "Sending notification about state transition");
30 if let Err(err) = self.broadcast.send(state) {
32 debug!(
33 err = %err.fmt_compact(),
34 %queue_len,
35 receivers=self.broadcast.receiver_count(),
36 "Could not send state transition notification, no active receivers"
37 );
38 }
39 }
40
41 pub fn module_notifier<S>(
44 &self,
45 module_instance: ModuleInstanceId,
46 client: FinalClientIface,
47 ) -> ModuleNotifier<S>
48 where
49 S: fedimint_client_module::sm::State,
50 {
51 ModuleNotifier::new(self.broadcast.clone(), module_instance, client)
52 }
53
54 pub fn sender(&self) -> NotifierSender {
57 NotifierSender {
58 sender: self.broadcast.clone(),
59 }
60 }
61}
62
63pub struct NotifierSender {
68 sender: tokio::sync::broadcast::Sender<DynState>,
69}
70
71impl NotifierSender {
72 pub fn notify(&self, state: DynState) {
74 let _res = self.sender.send(state);
75 }
76}