1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use async_trait::async_trait;
use std::{fmt::Debug, sync::mpsc::TryRecvError};

/// Publish an event `Ev` asynchronously from within a handler.
#[async_trait]
pub trait AsyncMediatorInternal<Ev: Debug> {
    async fn publish(&self, event: Ev);
}

/// Send a request `Req` asynchronously for processing to the mediator.
/// This will call the handler.
#[async_trait]
pub trait AsyncMediatorInternalHandle<Ev: Debug> {
    async fn send<Req>(&self, req: Req)
    where
        Req: Send,
        Self: AsyncRequestHandler<Req, Ev>;
}

/// Process the next event `Ev` from the channel asynchronously.
/// This will call all listeners with a clone of that event.
#[async_trait]
pub trait AsyncMediatorInternalNext {
    async fn next(&self) -> Result<(), TryRecvError>;
}

/// Handles the request `Req` asynchronously.
/// Implemented by the user.
#[async_trait]
pub trait AsyncRequestHandler<Req, Res>
where
    Self: Sync,
{
    async fn handle(&self, req: Req);
}