use std::sync::Arc;
use tracing::{debug, error, warn};
use crate::handler::{HandlerResponse, InputHandler};
use crate::types::KeelInput;
pub struct EventDispatcher {
handlers: Vec<Arc<dyn InputHandler>>,
}
impl EventDispatcher {
pub fn new() -> Self {
Self {
handlers: Vec::new(),
}
}
pub fn register(&mut self, handler: Arc<dyn InputHandler>) {
self.handlers.push(handler);
}
pub async fn dispatch(&self, input: &KeelInput) -> Vec<anyhow::Result<HandlerResponse>> {
let mut responses = Vec::new();
let mut handled = false;
for handler in &self.handlers {
if handler.handles(&input.input_type) {
handled = true;
debug!(
input_id = %input.id,
input_type = ?input.input_type,
"Dispatching to handler"
);
match handler.handle(input).await {
Ok(response) => {
debug!(
input_id = %input.id,
response = ?response,
"Handler responded"
);
responses.push(Ok(response));
}
Err(e) => {
error!(
input_id = %input.id,
error = %e,
"Handler error"
);
responses.push(Err(e));
}
}
}
}
if !handled {
warn!(
input_id = %input.id,
input_type = ?input.input_type,
"No handler registered for input type"
);
}
responses
}
}
impl Default for EventDispatcher {
fn default() -> Self {
Self::new()
}
}