Skip to main content

handler

Attribute Macro handler 

Source
#[handler]
Expand description

Creates a projection handler from an async function.

This macro transforms an async function into a handler struct that implements the projection::Handler<P> trait for use with projections to build read models.

§Function Signature

The function must have this signature:

async fn handler_name(
    event: Event<EventType>,
    projection: &mut ProjectionType,
) -> anyhow::Result<()>

§Generated Code

For a function handle_money_deposited, the macro generates:

  • HandleMoneyDepositedHandler struct
  • handle_money_deposited() constructor function
  • projection::Handler<ProjectionType> trait implementation

§Example

use evento::metadata::Event;

#[evento::handler]
async fn handle_money_deposited(
    event: Event<MoneyDeposited>,
    projection: &mut AccountBalanceView,
) -> anyhow::Result<()> {
    projection.balance += event.data.amount;
    Ok(())
}

// Register with projection
let projection = Projection::<AccountBalanceView, _>::new::<BankAccount>("account-123")
    .handler(handle_money_deposited());

// Execute projection to get current state
let result = projection.execute(&executor).await?;