Skip to main content

EventPublisher

Struct EventPublisher 

Source
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

Source

pub fn new() -> Self

Creates a new event publisher

§Examples
use verdure_context::EventPublisher;

let publisher = EventPublisher::new();
Source

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

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

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 publish
  • context - Reference to the ApplicationContext
Source

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

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

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

pub fn listener_statistics(&self) -> HashMap<String, usize>

Gets statistics about registered listeners

§Returns

A map of event type names to listener counts

§Examples
use verdure_context::EventPublisher;

let publisher = EventPublisher::new();
let stats = publisher.listener_statistics();
println!("Listener statistics: {:?}", stats);

Trait Implementations§

Source§

impl Default for EventPublisher

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.