pub struct EventEmitter { /* private fields */ }Expand description
The EventEmitter struct is the core of the event handling system.
It allows registration of event handlers and emits events to these handlers.
§Examples
use async_events_emitter::{EventEmitter, Event, EventHandler};
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct MyCustomEvent;
#[derive(Debug, Clone)]
pub struct MyEventHandler;
impl Event for MyCustomEvent {};
#[async_trait]
impl EventHandler<MyCustomEvent> for MyEventHandler {
async fn handle_event(&mut self, event: MyCustomEvent) {
// Handle event data here..
println!("{:?}", event)
}
}
// Create a new EventEmitter
let mut event_emitter = EventEmitter::new();
let my_event_handler = MyEventHandler;
// Register event handlers
event_emitter.on::<MyCustomEvent>(my_event_handler);
// Emit an event
event_emitter.emit(MyCustomEvent);Implementations§
Source§impl EventEmitter
impl EventEmitter
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of the EventEmitter.
This function initializes the EventEmitter with empty event handlers. It’s ideal for
starting an event-driven application where you’ll be adding handlers dynamically.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new instance of the EventEmitter with a specified capacity.
This variant of the constructor allows you to specify an initial capacity for event listeners, which can be useful for performance optimization in scenarios where a large number of listeners are expected.
§Arguments
capacity: The initial capacity for storing event listeners.
§Examples
let mut event_emitter = EventEmitter::with_capacity(100);Sourcepub fn on<E: Event + 'static>(
&mut self,
handler: impl EventHandler<E> + 'static,
) -> ListenerId
pub fn on<E: Event + 'static>( &mut self, handler: impl EventHandler<E> + 'static, ) -> ListenerId
Registers a handler for a specific event type.
The on method allows you to register a handler that will be called every time an event of a specific type is emitted. This is useful for setting up listeners that should persist and handle all occurrences of an event.
§Type Parameters
- E: The type of the event to listen for. This type must implement the Event trait.
§Arguments
- handler: An implementation of the EventHandler
trait. This handler will be called for every emitted event of type E.
§Returns
Returns a ListenerId which can be used to remove the listener at a later time with the off method.
§Examples
use async_events_emitter::*;
#[derive(Debug, Clone)]
struct MyCustomEvent;
let mut event_emitter = EventEmitter::new();
event_emitter.on::<MyCustomEvent>(my_event_handler);Sourcepub fn once<E: Event + 'static>(
&mut self,
handler: impl EventHandler<E> + 'static,
) -> ListenerId
pub fn once<E: Event + 'static>( &mut self, handler: impl EventHandler<E> + 'static, ) -> ListenerId
Registers a handler for a specific event type, but only for the next occurrence.
This method is similar to on, but the handler will be automatically unregistered after it handles an event once.
§Type Parameters
- E: The type of the event to listen for.
§Arguments
- handler: The handler for the event, which will be called only once.
§Returns
Returns a ListenerId, which can be used to prematurely remove the listener if needed.
§Examples
use async_events_emitter::*;
#[derive(Debug, Clone)]
struct MyCustomEvent;
let mut event_emitter = EventEmitter::new();
event_emitter.once::<MyCustomEvent>(my_event_handler);Sourcepub fn emit<E>(&self, event: E)
pub fn emit<E>(&self, event: E)
Emits an event, triggering all registered handlers for its type.
This method is used to emit an event of type E, which will be handled by all the registered handlers for this type.
§Type Parameters
- E: The type of the event being emitted. Must implement Event and Clone and be Send and ’static.
§Arguments
- event: The event instance to emit.
Sourcepub fn off<E>(&mut self, listener_id: ListenerId)
pub fn off<E>(&mut self, listener_id: ListenerId)
An alias for EventEmitter::remove_listener() Removes a specific listener based on its ListenerId.
This method is used to unsubscribe a previously registered event handler. The handler will no longer receive event notifications.
§Arguments
- listener_id: The ListenerId of the handler to be removed. This ID is obtained when registering a handler using on or once.
Sourcepub fn remove_listener(&self, listener_id: ListenerId)
pub fn remove_listener(&self, listener_id: ListenerId)
Removes a listener from the internal events map based on its ListenerId.
This internal method provides the core functionality for unregistering an event handler. It is used by the public off method.
§Arguments
- listener_id: The ListenerId of the listener to be removed.
Sourcepub fn get_event_listeners<E>(&self) -> Option<Vec<Listener<E>>>
pub fn get_event_listeners<E>(&self) -> Option<Vec<Listener<E>>>
Retrieves all listeners for a specific event type.
This method returns a list of all registered listeners for a given event type E. It’s useful for debugging or introspection purposes.
§Type Parameters
- E: The type of the event.
§Returns
Returns an Option<Vec<Listener