pub struct EventDispatcher { /* private fields */ }
Expand description
High-performance event dispatcher
The main component of the Mod Events system. Thread-safe and optimized for high-performance event dispatch with minimal overhead.
§Example
use mod_events::{EventDispatcher, Event};
#[derive(Debug, Clone)]
struct MyEvent {
message: String,
}
impl Event for MyEvent {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let dispatcher = EventDispatcher::new();
dispatcher.on(|event: &MyEvent| {
println!("Received: {}", event.message);
});
dispatcher.emit(MyEvent {
message: "Hello, World!".to_string(),
});
Implementations§
Source§impl EventDispatcher
impl EventDispatcher
Sourcepub fn subscribe<T, F>(&self, listener: F) -> ListenerId
pub fn subscribe<T, F>(&self, listener: F) -> ListenerId
§Example
use mod_events::{EventDispatcher, Event};
#[derive(Debug, Clone)]
struct MyEvent {
message: String,
}
impl Event for MyEvent {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let dispatcher = EventDispatcher::new();
dispatcher.subscribe(|event: &MyEvent| {
// Handle event, can return errors
if event.message.is_empty() {
return Err("Message cannot be empty".into());
}
println!("Message: {}", event.message);
Ok(())
});
Sourcepub fn subscribe_with_priority<T, F>(
&self,
listener: F,
priority: Priority,
) -> ListenerId
pub fn subscribe_with_priority<T, F>( &self, listener: F, priority: Priority, ) -> ListenerId
Subscribe to an event with a specific priority
Sourcepub fn on<T, F>(&self, listener: F) -> ListenerId
pub fn on<T, F>(&self, listener: F) -> ListenerId
Subscribe to an event with simple closure (no error handling)
This is the most convenient method for simple event handling.
§Example
use mod_events::{EventDispatcher, Event};
#[derive(Debug, Clone)]
struct MyEvent {
message: String,
}
impl Event for MyEvent {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let dispatcher = EventDispatcher::new();
dispatcher.on(|event: &MyEvent| {
println!("Received: {}", event.message);
});
Sourcepub fn subscribe_async<T, F, Fut>(&self, listener: F) -> ListenerId
pub fn subscribe_async<T, F, Fut>(&self, listener: F) -> ListenerId
Subscribe to an async event (requires “async” feature)
Sourcepub fn subscribe_async_with_priority<T, F, Fut>(
&self,
listener: F,
priority: Priority,
) -> ListenerId
pub fn subscribe_async_with_priority<T, F, Fut>( &self, listener: F, priority: Priority, ) -> ListenerId
Subscribe to an async event with priority (requires “async” feature)
Sourcepub fn dispatch<T: Event>(&self, event: T) -> DispatchResult
pub fn dispatch<T: Event>(&self, event: T) -> DispatchResult
Dispatch an event synchronously
Returns a DispatchResult
containing information about the dispatch.
§Example
use mod_events::{EventDispatcher, Event};
#[derive(Debug, Clone)]
struct MyEvent {
message: String,
}
impl Event for MyEvent {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let dispatcher = EventDispatcher::new();
let result = dispatcher.dispatch(MyEvent {
message: "Hello".to_string(),
});
if result.all_succeeded() {
println!("All listeners handled the event successfully");
}
Sourcepub async fn dispatch_async<T: Event>(&self, event: T) -> DispatchResult
pub async fn dispatch_async<T: Event>(&self, event: T) -> DispatchResult
Dispatch an event asynchronously (requires “async” feature)
Sourcepub fn emit<T: Event>(&self, event: T)
pub fn emit<T: Event>(&self, event: T)
Fire and forget - dispatch without waiting for results
This is the most efficient way to dispatch events when you don’t need to check the results.
§Example
use mod_events::{EventDispatcher, Event};
#[derive(Debug, Clone)]
struct MyEvent {
message: String,
}
impl Event for MyEvent {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let dispatcher = EventDispatcher::new();
dispatcher.emit(MyEvent {
message: "Fire and forget".to_string(),
});
Sourcepub fn add_middleware<F>(&self, middleware: F)
pub fn add_middleware<F>(&self, middleware: F)
Add middleware that can block events
Middleware functions receive events and return true
to allow
processing or false
to block the event.
§Example
use mod_events::{EventDispatcher, Event};
let dispatcher = EventDispatcher::new();
dispatcher.add_middleware(|event: &dyn Event| {
println!("Processing event: {}", event.event_name());
true // Allow all events
});
Sourcepub fn unsubscribe(&self, listener_id: ListenerId) -> bool
pub fn unsubscribe(&self, listener_id: ListenerId) -> bool
Remove a listener
Returns true
if the listener was found and removed, false
otherwise.
Sourcepub fn listener_count<T: Event + 'static>(&self) -> usize
pub fn listener_count<T: Event + 'static>(&self) -> usize
Get the number of listeners for an event type
Sourcepub fn metrics(&self) -> HashMap<TypeId, EventMetadata>
pub fn metrics(&self) -> HashMap<TypeId, EventMetadata>
Get event metrics