Skip to main content

Module events

Module events 

Source
Expand description

Domain event bus — typed pub-sub for application-level events decoupled from the ORM. Use this for cross-component fanout (OrderPlaced → mail + billing + audit handlers); use signals for per-Model lifecycle hooks. See events::EventBus. Domain event bus — typed pub-sub for application-level events decoupled from the ORM. Closes future-backlog item #45 (“internal event bus / domain event dispatch decoupled from ORM signals”).

§When to use this vs crate::signals

  • signals — per-Model lifecycle hooks (pre/post save, pre/post delete). Tied to ORM write paths. Use when you need “every time row X changes, do Y.”
  • events — arbitrary application events not tied to a specific Model. Use when you need “publish that an order was placed and let mail / billing / audit each react in their own subscriber.” Decouples cross-component fanout.

Same shape (typed, async, multi-subscriber, sequential dispatch); different intent.

§Quick start

ⓘ
use rustango::events::EventBus;
use std::sync::Arc;

#[derive(Clone)]
struct OrderPlaced { order_id: i64, total_cents: i64 }

let bus = EventBus::new();

// Subscribe (anywhere — main, app init, a service constructor):
bus.subscribe::<OrderPlaced, _>(|e| Box::pin(async move {
    println!("billing: charging {} cents for order {}", e.total_cents, e.order_id);
})).await;

// Publish (from a handler, a job, a worker):
bus.publish(OrderPlaced { order_id: 42, total_cents: 9999 }).await;

§Semantics

  • Subscribers run sequentially, in subscription order, awaited one at a time. (For parallel fanout, wrap a subscriber body in tokio::spawn.)
  • The event is Cloned once per subscriber so each receives an owned value. E: Clone + Send + Sync + 'static is required.
  • A panicking subscriber aborts the dispatch chain and propagates to the caller of publish. Use tokio::spawn for isolation if that’s a concern.
  • The bus is cheap to clone — internal state is Arc-shared. Pass clones into axum State, services, etc.

Structs§

EventBus
In-process domain event bus. Cheap to clone — internal state is Arc-shared.
SubscriberId
Opaque identifier returned by EventBus::subscribe for later use with EventBus::unsubscribe.

Type Aliases§

HandlerFuture
Future returned by event handlers. 'static because the handler is stored as Arc<dyn ...> and may run after the caller returns.