soil_client/utils/
notification.rs1use futures::stream::{FusedStream, Stream};
18use std::{
19 pin::Pin,
20 task::{Context, Poll},
21};
22
23use crate::utils::pubsub::{Hub, Receiver};
24
25mod registry;
26use registry::Registry;
27
28#[cfg(test)]
29mod tests;
30
31pub trait TracingKeyStr {
34 const TRACING_KEY: &'static str;
37}
38
39#[derive(Clone)]
44pub struct NotificationStream<Payload, TK: TracingKeyStr> {
45 hub: Hub<Payload, Registry>,
46 _pd: std::marker::PhantomData<TK>,
47}
48
49#[derive(Debug)]
51pub struct NotificationReceiver<Payload> {
52 receiver: Receiver<Payload, Registry>,
53}
54
55pub struct NotificationSender<Payload> {
57 hub: Hub<Payload, Registry>,
58}
59
60impl<Payload, TK: TracingKeyStr> NotificationStream<Payload, TK> {
61 pub fn channel() -> (NotificationSender<Payload>, Self) {
63 let hub = Hub::new(TK::TRACING_KEY);
64 let sender = NotificationSender { hub: hub.clone() };
65 let receiver = NotificationStream { hub, _pd: Default::default() };
66 (sender, receiver)
67 }
68
69 pub fn subscribe(&self, queue_size_warning: usize) -> NotificationReceiver<Payload> {
71 let receiver = self.hub.subscribe((), queue_size_warning);
72 NotificationReceiver { receiver }
73 }
74}
75
76impl<Payload> NotificationSender<Payload> {
77 pub fn notify<Error>(
80 &self,
81 payload: impl FnOnce() -> Result<Payload, Error>,
82 ) -> Result<(), Error>
83 where
84 Payload: Clone,
85 {
86 self.hub.send(payload)
87 }
88}
89
90impl<Payload> Clone for NotificationSender<Payload> {
91 fn clone(&self) -> Self {
92 Self { hub: self.hub.clone() }
93 }
94}
95
96impl<Payload> Unpin for NotificationReceiver<Payload> {}
97
98impl<Payload> Stream for NotificationReceiver<Payload> {
99 type Item = Payload;
100
101 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Payload>> {
102 Pin::new(&mut self.get_mut().receiver).poll_next(cx)
103 }
104}
105
106impl<Payload> FusedStream for NotificationReceiver<Payload> {
107 fn is_terminated(&self) -> bool {
108 self.receiver.is_terminated()
109 }
110}