EventEmitter

Struct EventEmitter 

Source
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

Source

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.

Source

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

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

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

pub fn emit<E>(&self, event: E)
where E: Clone + Send + 'static + Event,

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

pub fn off<E>(&mut self, listener_id: ListenerId)
where E: Clone + Send + 'static + Event,

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

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

pub fn get_event_listeners<E>(&self) -> Option<Vec<Listener<E>>>
where E: Clone + Send + 'static + Event,

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>> containing all listeners for the event type E. Returns None if there are no listeners for this event type.

Source

pub fn listeners_count(&self) -> usize

Source

pub fn event_listeners_count<E>(&self) -> Option<usize>
where E: Clone + Send + 'static + Event,

Trait Implementations§

Source§

impl Default for EventEmitter

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.