use crate::{event_bus_internal::EventBusInternal, Publisher, Subscriber};
use std::sync::{Arc, Mutex};
#[cfg(test)]
mod tests;
pub struct EventBus<ContentType, TopicId: std::cmp::PartialEq + Clone> {
internal: Arc<EventBusInternal<ContentType, TopicId>>,
}
impl<ContentType, TopicId: std::cmp::PartialEq + Clone> EventBus<ContentType, TopicId> {
pub fn new() -> Self {
Self {
internal: Arc::new(EventBusInternal::new()),
}
}
pub fn add_subscriber_shared(
&self,
subscriber: Arc<Mutex<dyn Subscriber<ContentType, TopicId>>>,
) {
self.internal.add_subscriber_shared(subscriber);
}
pub fn add_subscriber<S>(&self, subscriber: S)
where
S: Subscriber<ContentType, TopicId> + 'static, {
self.internal.add_subscriber(subscriber);
}
pub fn add_publisher(
&self,
publisher: &mut dyn Publisher<ContentType, TopicId>,
source_id: Option<u64>,
) -> Result<(), &'static str> {
publisher.get_mut_emitter().set_bus(self, source_id)
}
pub fn publish(&self, event: ContentType, topic_id: Option<TopicId>, source_id: u64) {
self.internal.publish(event, topic_id, source_id);
}
pub fn get_internal(&self) -> Arc<EventBusInternal<ContentType, TopicId>> {
self.internal.clone()
}
}