use async_trait::async_trait;
use tokio::sync::{mpsc, oneshot};
use super::filter::SubscriptionFilter;
#[derive(Clone, Debug)]
pub struct SubscriptionBroadcast<R> {
pub id: usize,
pub topic: String,
pub message: R,
}
#[derive(Clone, Debug)]
pub struct SubscriptionResponse {
pub id: usize,
}
pub type SubscriptionBroadcastTx<R> = mpsc::Sender<SubscriptionBroadcast<R>>;
pub type SubscriptionBroadcastRx<R> = mpsc::Receiver<SubscriptionBroadcast<R>>;
pub type SubscriptionResponseTx = oneshot::Sender<SubscriptionResponse>;
#[async_trait]
pub trait SubscriptionHandler<R>
where
R: Clone + Send + 'static,
{
async fn subscribe(&mut self, topic: String, respond_to: SubscriptionResponseTx);
async fn subscribe_with_filter(
&mut self,
topic: String,
filter: Box<dyn SubscriptionFilter<R> + Send + Sync>,
respond_to: SubscriptionResponseTx,
);
async fn unsubscribe(&mut self, id: usize, respond_to: SubscriptionResponseTx);
async fn broadcast(&self, topic: String, message: R) -> Result<(), anyhow::Error>;
}