pub struct EventBus<T> { /* private fields */ }Expand description
An asynchronous EventBus to interact with.
Implementations§
Source§impl<T> EventBus<T>
impl<T> EventBus<T>
Sourcepub async fn subscribe(
&self,
event_type: &str,
handler: Handler<T>,
) -> HandlerId
pub async fn subscribe( &self, event_type: &str, handler: Handler<T>, ) -> HandlerId
Subscribe to an event type.
It takes the event type as a string and a handler implementing the Handle<T> trait.
The method returns a HandlerId that uniquely identifies the handler within the event bus.
§Example
struct MyEventData {
// Define your event data structure here
}
struct MyEventHandler;
#[async_trait]
impl Handle<MyEventData> for MyEventHandler {
async fn handle(&self, event: &Event<MyEventData>) -> Result<(), BasuError> {
// Handle the event here
// ...
Ok(())
}
}
let event_bus = EventBus::<MyEventData>::new();
let handler = MyEventHandler;
let handler_id = event_bus.subscribe("my_event", Box::new(handler)).await;Sourcepub async fn unsubscribe(
&self,
event_type: &str,
handler_id: &HandlerId,
) -> Result<(), BasuError>
pub async fn unsubscribe( &self, event_type: &str, handler_id: &HandlerId, ) -> Result<(), BasuError>
Unsubscribe handler from an event type.
It takes the event type and the HandlerId of the handler to be removed.
struct MyEventData {
// Define your event data structure here
}
struct MyEventHandler;
#[async_trait]
impl Handle<MyEventData> for MyEventHandler {
async fn handle(&self, event: &Event<MyEventData>) -> Result<(), BasuError> {
// Handle the event here
// ...
Ok(())
}
}
let event_bus = EventBus::<MyEventData>::new();
let handler = MyEventHandler;
let handler_id = event_bus.subscribe("my_event", Box::new(handler)).await;
event_bus.unsubscribe("my_event", &handler_id).await?;Sourcepub async fn publish(
&self,
event_type: &str,
event_data: &Event<T>,
) -> Result<(), BasuError>
pub async fn publish( &self, event_type: &str, event_data: &Event<T>, ) -> Result<(), BasuError>
Publish an event to subscribed handlers,
It takes the event type and an Event<T> instance containing the event data.
struct MyEventData {
// Define your event data structure here
}
struct MyEventHandler;
#[async_trait]
impl Handle<MyEventData> for MyEventHandler {
async fn handle(&self, event: &Event<MyEventData>) -> Result<(), BasuError> {
// Handle the event here
// ...
Ok(())
}
}
let event_bus = EventBus::<MyEventData>::new();
let handler = MyEventHandler;
let handler_id = event_bus.subscribe("my_event", Box::new(handler)).await;
let event_data = MyEventData { /* initialize your event data */ };
let event = Event::new(event_data);
event_bus.publish("my_event", &event).await?;Sourcepub async fn list(&self) -> Vec<String>
pub async fn list(&self) -> Vec<String>
List all registered event types. It returns a Vec that contains the names of the registered event types.
struct MyEventData {
// Define your event data structure here
}
struct MyEventHandler;
#[async_trait]
impl Handle<MyEventData> for MyEventHandler {
async fn handle(&self, event: &Event<MyEventData>) -> Result<(), BasuError> {
// Handle the event here
// ...
Ok(())
}
}
let event_bus = EventBus::<MyEventData>::new();
let handler = MyEventHandler;
let _handler_id = event_bus.subscribe("my_event", Box::new(handler)).await;
let event_types = event_bus.list().await;
for event_type in event_types {
println!("Registered event type: {}", event_type);
}Sourcepub async fn get_handler_count(
&self,
event_type: &str,
) -> Result<usize, BasuError>
pub async fn get_handler_count( &self, event_type: &str, ) -> Result<usize, BasuError>
Get the number of registered handlers for a specific event type.
struct MyEventData {
// Define your event data structure here
}
let event_bus = EventBus::<EventData>::new();
let event_type = "my_event";
let handler_count = event_bus.get_handler_count(event_type).await?;
println!("Number of handlers for event '{}': {}", event_type, handler_count);Sourcepub async fn clear(&self)
pub async fn clear(&self)
Clear all event handlers from the event bus. It removes all registered event handlers.
struct MyEventData {
// Define your event data structure here
}
struct MyEventHandler;
#[async_trait]
impl Handle<MyEventData> for MyEventHandler {
async fn handle(&self, event: &Event<MyEventData>) -> Result<(), BasuError> {
// Handle the event here
// ...
Ok(())
}
}
let event_bus = EventBus::<MyEventData>::new();
let handler = MyEventHandler;
let _handler_id = event_bus.subscribe("my_event", Box::new(handler)).await;
event_bus.clear().await;
println!("All event handlers cleared");Note: The clear method removes all event handlers and makes the event bus empty.