pub struct EventBus { /* private fields */ }Expand description
In-process publish/subscribe event bus
Supports both synchronous and asynchronous event handlers. Multiple handlers can be registered for the same event topic.
§Example
use rustapi_core::events::EventBus;
let bus = EventBus::new();
// Synchronous handler
bus.on("user.created", |payload: &str| {
println!("User created: {}", payload);
});
// Emit events
bus.emit("user.created", "user_123");Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn on<F>(&self, topic: &str, handler: F)
pub fn on<F>(&self, topic: &str, handler: F)
Register a synchronous event handler for a topic
The handler will be called with the event payload string whenever the topic is emitted.
§Example
use rustapi_core::events::EventBus;
let bus = EventBus::new();
bus.on("order.completed", |payload: &str| {
println!("Order completed: {}", payload);
});Sourcepub fn emit(&self, topic: &str, payload: &str)
pub fn emit(&self, topic: &str, payload: &str)
Emit an event synchronously
Calls all synchronous handlers for the topic in registration order. Also spawns tokio tasks for any async handlers.
§Example
use rustapi_core::events::EventBus;
let bus = EventBus::new();
bus.on("log", |msg: &str| println!("{}", msg));
bus.emit("log", "Hello!");Sourcepub async fn emit_await(&self, topic: &str, payload: &str)
pub async fn emit_await(&self, topic: &str, payload: &str)
Emit an event and await all async handlers
Unlike emit(), this waits for all async handlers to complete.
Sourcepub fn handler_count(&self, topic: &str) -> usize
pub fn handler_count(&self, topic: &str) -> usize
Get the number of registered handlers for a topic (both sync and async)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EventBus
impl RefUnwindSafe for EventBus
impl Send for EventBus
impl Sync for EventBus
impl Unpin for EventBus
impl UnsafeUnpin for EventBus
impl UnwindSafe for EventBus
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more