Struct EventBus

Source
pub struct EventBus<T> { /* private fields */ }
Expand description

An asynchronous EventBus to interact with.

Implementations§

Source§

impl<T> EventBus<T>

Source

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;
Source

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?;
Source

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?;
Source

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);
}
Source

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);
Source

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.

Source§

impl<T> EventBus<T>

Source

pub fn new() -> Self

create a new EventBus

Trait Implementations§

Source§

impl<T: Default> Default for EventBus<T>

Source§

fn default() -> EventBus<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for EventBus<T>

§

impl<T> !RefUnwindSafe for EventBus<T>

§

impl<T> Send for EventBus<T>

§

impl<T> Sync for EventBus<T>

§

impl<T> Unpin for EventBus<T>

§

impl<T> !UnwindSafe for EventBus<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V