use tokio_events::{Event, EventBus};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ImportantEvent {
id: u64,
data: String,
}
impl Event for ImportantEvent {
fn event_type() -> &'static str {
"ImportantEvent"
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = tempfile::tempdir()?;
let db_path = temp_dir.path().join("events.redb");
let bus = EventBus::builder().with_redb_path(&db_path).build().await?;
let handle = bus
.subscribe(|event: ImportantEvent| async move {
println!("Subscriber processed event: {:?}", event);
})
.await?;
println!("Publishing ImportantEvent...");
bus.publish(ImportantEvent {
id: 42,
data: "Highly critical data!".to_string(),
})
.await?;
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
bus.unsubscribe(handle).await?;
bus.shutdown().await?;
Ok(())
}