#[handler]Expand description
Creates an event handler for use with event subscriptions
The #[evento::handler(AggregateType)] attribute macro transforms a function into an event handler
that can be used with [evento::subscribe]. The macro generates the necessary boilerplate to
integrate with the subscription system.
§Syntax
ⓘ
#[evento::handler(AggregateType)]
async fn handler_name<E: evento::Executor>(
context: &evento::Context<'_, E>,
event: EventDetails<EventType>,
) -> anyhow::Result<()> {
// Handler logic
Ok(())
}§Parameters
AggregateType: The aggregate type this handler is associated with
§Function Requirements
The decorated function must:
- Be
async - Take
&evento::Context<'_, E>as first parameter whereE: evento::Executor - Take
EventDetails<SomeEventType>as second parameter - Return
anyhow::Result<()>
§Examples
use evento::{Context, EventDetails, Executor};
use bincode::{Encode, Decode};
#[evento::handler(User)]
async fn on_user_created<E: Executor>(
context: &Context<'_, E>,
event: EventDetails<UserCreated>,
) -> anyhow::Result<()> {
println!("User created: {}", event.data.name);
// Can trigger side effects, call external services, etc.
Ok(())
}
// Use with subscription
evento::subscribe("user-handlers")
.aggregator::<User>()
.handler(on_user_created())
.run(&executor)
.await?;§Generated Code
The macro generates:
- A struct implementing [
evento::SubscribeHandler] - A constructor function returning an instance of that struct
- Type-safe event filtering based on the event type