docbox_core/notifications/
mpsc.rs

1use super::{NotificationQueue, NotificationQueueMessage};
2use tokio::sync::mpsc;
3
4/// In-process notification queue, used for a webhook based system when using
5/// a webhook endpoint on the server as a notification source
6pub struct MpscNotificationQueue {
7    rx: mpsc::Receiver<NotificationQueueMessage>,
8
9    /// Sender held by the queue until its consumed
10    sender: Option<MpscNotificationQueueSender>,
11}
12
13#[derive(Clone)]
14pub struct MpscNotificationQueueSender {
15    tx: mpsc::Sender<NotificationQueueMessage>,
16}
17
18impl MpscNotificationQueueSender {
19    pub async fn send(&self, msg: NotificationQueueMessage) {
20        _ = self.tx.send(msg).await;
21    }
22}
23
24impl MpscNotificationQueue {
25    pub fn create() -> MpscNotificationQueue {
26        let (tx, rx) = mpsc::channel(10);
27        MpscNotificationQueue {
28            rx,
29            sender: Some(MpscNotificationQueueSender { tx }),
30        }
31    }
32
33    pub fn take_sender(&mut self) -> Option<MpscNotificationQueueSender> {
34        self.sender.take()
35    }
36}
37
38impl NotificationQueue for MpscNotificationQueue {
39    async fn next_message(&mut self) -> Option<NotificationQueueMessage> {
40        self.rx.recv().await
41    }
42}