handler

Attribute Macro handler 

Source
#[handler]
Expand description

Creates an event handler from an async function.

This macro transforms an async function into a handler struct that implements the Handler<P, E> trait for use with projections.

§Function Signature

The function must have this signature:

async fn handler_name<E: Executor>(
    event: Event<EventType>,
    action: Action<'_, ProjectionType, E>,
) -> anyhow::Result<()>

§Generated Code

For a function handle_money_deposited, the macro generates:

  • HandleMoneyDepositedHandler struct
  • handle_money_deposited() constructor function
  • Handler<ProjectionType, E> trait implementation

§Example

#[evento::handler]
async fn handle_money_deposited<E: Executor>(
    event: Event<MoneyDeposited>,
    action: Action<'_, AccountBalanceView, E>,
) -> anyhow::Result<()> {
    match action {
        Action::Apply(row) => {
            row.balance += event.data.amount;
        }
        Action::Handle(_context) => {
            // Side effects, notifications, etc.
        }
    };
    Ok(())
}

// Register with projection
let projection = Projection::new("account-balance")
    .handler(handle_money_deposited());

§Action Variants

  • Action::Apply(row) - Mutate projection state (for rebuilding from events)
  • Action::Handle(context) - Handle side effects during live processing