use super::{NotificationQueue, NotificationQueueMessage};
use tokio::sync::mpsc;
pub struct MpscNotificationQueue {
rx: mpsc::Receiver<NotificationQueueMessage>,
sender: Option<MpscNotificationQueueSender>,
}
#[derive(Clone)]
pub struct MpscNotificationQueueSender {
tx: mpsc::Sender<NotificationQueueMessage>,
}
impl MpscNotificationQueueSender {
pub async fn send(&self, msg: NotificationQueueMessage) {
_ = self.tx.send(msg).await;
}
}
impl MpscNotificationQueue {
pub fn create() -> MpscNotificationQueue {
let (tx, rx) = mpsc::channel(10);
MpscNotificationQueue {
rx,
sender: Some(MpscNotificationQueueSender { tx }),
}
}
pub fn take_sender(&mut self) -> Option<MpscNotificationQueueSender> {
self.sender.take()
}
}
impl NotificationQueue for MpscNotificationQueue {
async fn next_message(&mut self) -> Option<NotificationQueueMessage> {
self.rx.recv().await
}
}