Skip to main content

ghostscope_ui/events/
channels.rs

1use super::runtime::{RuntimeCommand, RuntimeStatus};
2use super::trace_display::UiTraceEvent;
3use tokio::sync::mpsc;
4
5/// Registry for event communication between TUI and runtime
6#[derive(Debug)]
7pub struct EventRegistry {
8    // TUI -> Runtime communication
9    pub command_sender: mpsc::UnboundedSender<RuntimeCommand>,
10
11    // Runtime -> TUI communication
12    pub trace_receiver: mpsc::Receiver<UiTraceEvent>,
13    pub status_receiver: mpsc::UnboundedReceiver<RuntimeStatus>,
14}
15
16impl EventRegistry {
17    pub fn new() -> (Self, RuntimeChannels) {
18        Self::new_with_trace_capacity(DEFAULT_TRACE_CHANNEL_CAPACITY)
19    }
20
21    pub fn new_with_trace_capacity(trace_capacity: usize) -> (Self, RuntimeChannels) {
22        let trace_capacity = trace_capacity.max(1);
23        let (command_tx, command_rx) = mpsc::unbounded_channel();
24        let (trace_tx, trace_rx) = mpsc::channel::<UiTraceEvent>(trace_capacity);
25        let (status_tx, status_rx) = mpsc::unbounded_channel();
26
27        let registry = EventRegistry {
28            command_sender: command_tx,
29            trace_receiver: trace_rx,
30            status_receiver: status_rx,
31        };
32
33        let channels = RuntimeChannels {
34            command_receiver: command_rx,
35            trace_sender: trace_tx.clone(),
36            status_sender: status_tx.clone(),
37            trace_channel_capacity: trace_capacity,
38        };
39
40        (registry, channels)
41    }
42}
43
44/// Default queue size for runtime->UI trace events.
45pub const DEFAULT_TRACE_CHANNEL_CAPACITY: usize = 4096;
46
47/// Channels used by the runtime to receive commands and send events
48#[derive(Debug)]
49pub struct RuntimeChannels {
50    pub command_receiver: mpsc::UnboundedReceiver<RuntimeCommand>,
51    pub trace_sender: mpsc::Sender<UiTraceEvent>,
52    pub status_sender: mpsc::UnboundedSender<RuntimeStatus>,
53    pub trace_channel_capacity: usize,
54}
55
56impl RuntimeChannels {
57    /// Create a status sender that can be shared with other tasks
58    pub fn create_status_sender(&self) -> mpsc::UnboundedSender<RuntimeStatus> {
59        self.status_sender.clone()
60    }
61
62    /// Create a trace sender that can be shared with other tasks
63    pub fn create_trace_sender(&self) -> mpsc::Sender<UiTraceEvent> {
64        self.trace_sender.clone()
65    }
66}