Skip to main content

reovim_kernel/ipc/event_bus/
sender.rs

1//! Cloneable sender handle for emitting events.
2
3use super::super::{
4    channel::BoundedSender,
5    event::{DynEvent, Event},
6    scope::EventScope,
7};
8
9/// Cloneable sender handle for emitting events via channel.
10///
11/// Obtained from `EventBus::sender()` when the bus is created with
12/// `new_with_channel()`.
13///
14/// # Thread Safety
15///
16/// `EventSender` is `Clone`, `Send`, and `Sync`. Multiple threads can
17/// send events concurrently.
18#[derive(Clone)]
19pub struct EventSender {
20    pub(super) tx: BoundedSender<DynEvent>,
21}
22
23impl EventSender {
24    /// Send an event (blocking if channel is full).
25    ///
26    /// This will block if the channel is full until space is available.
27    pub fn send<E: Event>(&self, event: E) {
28        let _ = self.tx.send(DynEvent::new(event));
29    }
30
31    /// Try to send an event without blocking.
32    ///
33    /// Returns immediately even if the channel is full.
34    pub fn try_send<E: Event>(&self, event: E) {
35        let _ = self.tx.try_send(DynEvent::new(event));
36    }
37
38    /// Send a pre-boxed dynamic event.
39    pub fn send_dyn(&self, event: DynEvent) {
40        let _ = self.tx.try_send(event);
41    }
42
43    /// Send an event with scope tracking.
44    ///
45    /// The scope is incremented before sending.
46    pub fn send_scoped<E: Event>(&self, event: E, scope: &EventScope) {
47        scope.increment();
48        let dyn_event = DynEvent::new(event).with_scope(scope.clone());
49        let _ = self.tx.try_send(dyn_event);
50    }
51}