use std::sync::mpsc::TryRecvError;
use async_std::sync::Mutex;
use async_trait::async_trait;
use std::fmt::Debug;
use crate::asynchronous::basic::BasicAsyncMediator;
use super::*;
#[cfg(feature = "async")]
#[derive(Debug)]
pub struct CxAwareAsyncMediator<Cx, Ev>
where
Cx: Debug,
Ev: Debug + 'static,
{
pub(crate) basic: BasicAsyncMediator<Ev>,
pub(crate) cx: Mutex<Cx>,
}
#[async_trait]
impl<Cx, Ev> AsyncMediatorInternal<Ev> for CxAwareAsyncMediator<Cx, Ev>
where
Cx: Debug + Send,
Ev: Debug + Send,
{
async fn publish(&self, event: Ev) {
self.basic.publish(event).await
}
}
#[async_trait]
impl<Cx, Ev> CxAwareAsyncMediatorInternalHandle<Cx, Ev> for CxAwareAsyncMediator<Cx, Ev>
where
Cx: Debug + Send + Sync,
Ev: Debug + Send,
{
async fn send<Req>(&self, req: Req)
where
Self: CxAwareAsyncRequestHandler<Cx, Req, Ev>,
Req: Send,
{
let m = self.cx.lock().await;
<Self as CxAwareAsyncRequestHandler<Cx, Req, Ev>>::handle(self, req, &m).await
}
}
#[async_trait]
impl<Cx, Ev> AsyncMediatorInternalNext for CxAwareAsyncMediator<Cx, Ev>
where
Cx: Debug + Send,
Ev: Debug + Send,
{
async fn next(&self) -> Result<(), TryRecvError> {
self.basic.next().await
}
}