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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more