Skip to main content

Listener

Trait Listener 

Source
pub trait Listener<E>:
    Send
    + Sync
    + 'static
where E: Event,
{ // Required method fn handle<'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 E, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; // Provided methods fn name(&self) -> &'static str { ... } fn should_stop_propagation(&self) -> bool { ... } }
Expand description

A listener that handles events of type E.

Listeners contain the logic that should run when an event is dispatched. They can be synchronous or asynchronous.

§Example

use ferro_events::{Event, Listener, Error, async_trait};

#[derive(Clone)]
struct UserRegistered { email: String }

impl Event for UserRegistered {
    fn name(&self) -> &'static str { "UserRegistered" }
}

struct SendWelcomeEmail;

#[async_trait]
impl Listener<UserRegistered> for SendWelcomeEmail {
    async fn handle(&self, event: &UserRegistered) -> Result<(), Error> {
        println!("Welcome, {}!", event.email);
        Ok(())
    }
}

Required Methods§

Source

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 E, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Handle the event.

This method is called when the event is dispatched. It receives an immutable reference to the event data.

Provided Methods§

Source

fn name(&self) -> &'static str

Returns the name of the listener for logging and debugging.

Source

fn should_stop_propagation(&self) -> bool

Whether this listener should stop propagation to other listeners.

If this returns true after handling, no further listeners will be called for this event.

Implementors§