docbox_core/notifications/
mpsc.rs1use super::{NotificationQueue, NotificationQueueMessage};
2use tokio::sync::mpsc;
3
4pub struct MpscNotificationQueue {
7 rx: mpsc::Receiver<NotificationQueueMessage>,
8
9 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}