use crate::callbacks::types::event::{EventState, EventType};
use pyo3::prelude::*;
use tokio::sync::mpsc;
pub trait EventCallbackInterface {
fn handle_event(&mut self, event_type: EventType, event_data: EventState);
}
#[derive(Clone, Debug)]
#[pyclass]
pub struct EventHandler {
event_sender: Option<mpsc::Sender<(EventType, EventState)>>,
}
impl EventHandler {
pub fn new(event_sender: Option<mpsc::Sender<(EventType, EventState)>>) -> Self {
EventHandler { event_sender }
}
}
#[derive(Clone, Debug)]
#[pyclass]
pub struct PyEventCallbackInterface {
event_handler: EventHandler,
}
#[pymethods]
impl PyEventCallbackInterface {
#[new]
pub fn new(event_handler: EventHandler) -> Self {
PyEventCallbackInterface { event_handler }
}
fn handle_event(&mut self, event_type: EventType, event_data: EventState) {
self.event_handler.handle_event(event_type, event_data);
}
}
impl EventCallbackInterface for EventHandler {
fn handle_event(&mut self, event_type: EventType, event_data: EventState) {
if let Some(event_sender) = &self.event_sender {
event_sender.try_send((event_type, event_data)).unwrap_or_else(|e| {
println!("Error sending event: {:?}", e);
});
} else {
println!("Event sender is None");
println!("Event type: {:?}", event_type);
println!("Event data: {:?}", event_data);
}
}
}