use super::runtime::{RuntimeCommand, RuntimeStatus};
use super::trace_display::UiTraceEvent;
use tokio::sync::mpsc;
#[derive(Debug)]
pub struct EventRegistry {
pub command_sender: mpsc::UnboundedSender<RuntimeCommand>,
pub trace_receiver: mpsc::Receiver<UiTraceEvent>,
pub status_receiver: mpsc::UnboundedReceiver<RuntimeStatus>,
}
impl EventRegistry {
pub fn new() -> (Self, RuntimeChannels) {
Self::new_with_trace_capacity(DEFAULT_TRACE_CHANNEL_CAPACITY)
}
pub fn new_with_trace_capacity(trace_capacity: usize) -> (Self, RuntimeChannels) {
let trace_capacity = trace_capacity.max(1);
let (command_tx, command_rx) = mpsc::unbounded_channel();
let (trace_tx, trace_rx) = mpsc::channel::<UiTraceEvent>(trace_capacity);
let (status_tx, status_rx) = mpsc::unbounded_channel();
let registry = EventRegistry {
command_sender: command_tx,
trace_receiver: trace_rx,
status_receiver: status_rx,
};
let channels = RuntimeChannels {
command_receiver: command_rx,
trace_sender: trace_tx.clone(),
status_sender: status_tx.clone(),
trace_channel_capacity: trace_capacity,
};
(registry, channels)
}
}
pub const DEFAULT_TRACE_CHANNEL_CAPACITY: usize = 4096;
#[derive(Debug)]
pub struct RuntimeChannels {
pub command_receiver: mpsc::UnboundedReceiver<RuntimeCommand>,
pub trace_sender: mpsc::Sender<UiTraceEvent>,
pub status_sender: mpsc::UnboundedSender<RuntimeStatus>,
pub trace_channel_capacity: usize,
}
impl RuntimeChannels {
pub fn create_status_sender(&self) -> mpsc::UnboundedSender<RuntimeStatus> {
self.status_sender.clone()
}
pub fn create_trace_sender(&self) -> mpsc::Sender<UiTraceEvent> {
self.trace_sender.clone()
}
}