pub struct EventPublisher { /* private fields */ }Expand description
Event publisher for broadcasting events
EventPublisher manages event listeners and provides functionality to publish
events to all registered listeners. It supports type-safe event handling and
allows multiple listeners for the same event type.
§Examples
use verdure_context::{EventPublisher, Event, EventListener};
use std::any::Any;
#[derive(Debug, Clone)]
struct TestEvent {
message: String,
}
impl Event for TestEvent {
fn name(&self) -> &'static str {
"TestEvent"
}
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}
struct TestListener;
impl EventListener<TestEvent> for TestListener {
fn on_event(&self, event: &TestEvent) {
println!("Received: {}", event.message);
}
}
let mut publisher = EventPublisher::new();
publisher.subscribe(TestListener);
let event = TestEvent {
message: "Hello, World!".to_string(),
};
publisher.publish(&event);Implementations§
Source§impl EventPublisher
impl EventPublisher
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new event publisher
§Examples
use verdure_context::EventPublisher;
let publisher = EventPublisher::new();Sourcepub fn subscribe_context_aware<T: Event + 'static, L: ContextAwareEventListener<T> + 'static>(
&self,
listener: L,
)
pub fn subscribe_context_aware<T: Event + 'static, L: ContextAwareEventListener<T> + 'static>( &self, listener: L, )
Subscribes a context-aware listener to events of type T
Context-aware listeners receive both the event and a reference to the ApplicationContext, allowing them to interact with the context during event handling.
§Arguments
listener- The context-aware event listener to register
§Examples
use verdure_context::{EventPublisher, ContextInitializedEvent, ContextAwareEventListener, ApplicationContext};
struct StartupListener;
impl ContextAwareEventListener<ContextInitializedEvent> for StartupListener {
fn on_context_event(&self, event: &ContextInitializedEvent, context: &ApplicationContext) {
println!("Context initialized with {} sources", event.config_sources_count);
println!("App name: {}", context.get_config("app.name"));
}
}
let mut publisher = EventPublisher::new();
publisher.subscribe_context_aware(StartupListener);Sourcepub fn subscribe<T: Event + 'static, L: EventListener<T> + 'static>(
&self,
listener: L,
)
pub fn subscribe<T: Event + 'static, L: EventListener<T> + 'static>( &self, listener: L, )
§Arguments
listener- The event listener to register
§Examples
use verdure_context::{EventPublisher, Event, EventListener};
use std::any::Any;
#[derive(Debug, Clone)]
struct MyEvent;
impl Event for MyEvent {
fn name(&self) -> &'static str { "MyEvent" }
fn as_any(&self) -> &dyn Any { self }
fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
}
struct MyListener;
impl EventListener<MyEvent> for MyListener {
fn on_event(&self, _event: &MyEvent) {
println!("Event received!");
}
}
let mut publisher = EventPublisher::new();
publisher.subscribe(MyListener);Sourcepub fn publish_with_context<T: Event + 'static>(
&self,
event: &T,
context: &ApplicationContext,
)
pub fn publish_with_context<T: Event + 'static>( &self, event: &T, context: &ApplicationContext, )
Publishes an event to all registered listeners with context access
This method publishes the event to both regular listeners and context-aware listeners. Context-aware listeners will receive a reference to the ApplicationContext.
§Arguments
event- The event to publishcontext- Reference to the ApplicationContext
Sourcepub fn publish<T: Event + 'static>(&self, event: &T)
pub fn publish<T: Event + 'static>(&self, event: &T)
§Arguments
event- The event to publish
§Examples
use verdure_context::{EventPublisher, Event, EventListener};
use std::any::Any;
#[derive(Debug, Clone)]
struct NotificationEvent {
message: String,
}
impl Event for NotificationEvent {
fn name(&self) -> &'static str { "Notification" }
fn as_any(&self) -> &dyn Any { self }
fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
}
let publisher = EventPublisher::new();
let event = NotificationEvent {
message: "System startup complete".to_string(),
};
publisher.publish(&event);Sourcepub fn listener_count<T: Event + 'static>(&self) -> usize
pub fn listener_count<T: Event + 'static>(&self) -> usize
Gets the number of listeners for a specific event type
§Returns
The number of listeners registered for the event type T
§Examples
use verdure_context::{EventPublisher, Event, EventListener};
use std::any::Any;
#[derive(Debug, Clone)]
struct CountEvent;
impl Event for CountEvent {
fn name(&self) -> &'static str { "CountEvent" }
fn as_any(&self) -> &dyn Any { self }
fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
}
struct CountListener;
impl EventListener<CountEvent> for CountListener {
fn on_event(&self, _event: &CountEvent) {}
}
let mut publisher = EventPublisher::new();
assert_eq!(publisher.listener_count::<CountEvent>(), 0);
publisher.subscribe(CountListener);
assert_eq!(publisher.listener_count::<CountEvent>(), 1);Sourcepub fn clear_all_listeners(&mut self)
pub fn clear_all_listeners(&mut self)
Removes all listeners for all event types
§Examples
use verdure_context::EventPublisher;
let mut publisher = EventPublisher::new();
publisher.clear_all_listeners();