pub struct EventBus { /* private fields */ }Expand description
Event bus for publishing and subscribing to node events
The EventBus is the central coordination point for the event system. It allows
components to publish events and provides two mechanisms for subscribing to events:
- Callback-based: Register an
EventSubscriberto receive events via callbacks - Channel-based: Get a
broadcast::Receiver<NodeEvent>for async event processing
§Thread Safety
The EventBus is designed to be thread-safe, with all mutable state protected
by appropriate synchronization primitives. It can be safely shared across threads
using Arc<EventBus>.
§Example Usage
use std::sync::Arc;
use tap_node::event::{EventBus, NodeEvent};
async fn example() {
// Create a new event bus
let event_bus = Arc::new(EventBus::new());
// Subscribe to events using a channel
let mut receiver = event_bus.subscribe_channel();
// Publish an event using public methods
let did = "did:example:123".to_string();
event_bus.publish_agent_registered(did).await;
// Process events from the channel
tokio::spawn(async move {
while let Ok(event) = receiver.recv().await {
println!("Received event: {:?}", event);
}
});
}Implementations§
Source§impl EventBus
impl EventBus
Sourcepub async fn subscribe(&self, subscriber: Arc<dyn EventSubscriber>)
pub async fn subscribe(&self, subscriber: Arc<dyn EventSubscriber>)
Subscribe to node events
Sourcepub fn subscribe_channel(&self) -> Receiver<NodeEvent>
pub fn subscribe_channel(&self) -> Receiver<NodeEvent>
Get a receiver for node events
Sourcepub async fn unsubscribe(&self, subscriber: &Arc<dyn EventSubscriber>)
pub async fn unsubscribe(&self, subscriber: &Arc<dyn EventSubscriber>)
Remove a subscriber from the event bus
Sourcepub async fn publish_message_received(&self, message: Message)
pub async fn publish_message_received(&self, message: Message)
Publish a message received event
Sourcepub async fn publish_message_sent(
&self,
message: Message,
from: String,
to: String,
)
pub async fn publish_message_sent( &self, message: Message, from: String, to: String, )
Publish a message sent event
Sourcepub async fn publish_agent_registered(&self, did: String)
pub async fn publish_agent_registered(&self, did: String)
Publish an agent registered event
Sourcepub async fn publish_agent_unregistered(&self, did: String)
pub async fn publish_agent_unregistered(&self, did: String)
Publish an agent unregistered event
Sourcepub async fn publish_agent_message(&self, did: String, message: Vec<u8>)
pub async fn publish_agent_message(&self, did: String, message: Vec<u8>)
Publish an agent message event
Sourcepub async fn publish_did_resolved(&self, did: String, success: bool)
pub async fn publish_did_resolved(&self, did: String, success: bool)
Publish a DID resolved event
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 !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