pub trait EventSubscriber: Send + Sync {
// Required method
fn handle_event<'life0, 'async_trait>(
&'life0 self,
event: NodeEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Event subscriber trait for receiving node events
This trait defines the interface for components that want to receive
node events via callbacks. Implementers must define the handle_event
method to process events as they occur.
§Thread Safety
All implementations must be Send + Sync to ensure they can be safely
used in multithreaded environments.
§Usage
use std::sync::Arc;
use async_trait::async_trait;
use tap_node::event::{EventSubscriber, NodeEvent, EventBus};
#[derive(Debug)]
struct MyEventHandler {
name: String,
}
#[async_trait]
impl EventSubscriber for MyEventHandler {
async fn handle_event(&self, event: NodeEvent) {
println!("Handler '{}' received event: {:?}", self.name, event);
}
}
async fn example(event_bus: &EventBus) {
let handler = Arc::new(MyEventHandler { name: "Logger".to_string() });
event_bus.subscribe(handler).await;
}Required Methods§
Sourcefn handle_event<'life0, 'async_trait>(
&'life0 self,
event: NodeEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_event<'life0, 'async_trait>(
&'life0 self,
event: NodeEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handle a node event
This method is called whenever an event is published to the event bus. Implementations should process the event appropriately based on its type.
§Parameters
event: The NodeEvent to handle
§Note
- This method should return quickly to avoid blocking the event bus
- For long-running operations, spawn a separate task
- Handle errors gracefully, as exceptions may disrupt the event system