ori_core/event/
sink.rs

1use std::{any::Any, fmt::Debug};
2
3use crate::{Event, Lock, Lockable, SendSync, Sendable, Shared};
4
5/// An event sender, that can send events to the application.
6///
7/// This is usually implemented by the application shell and
8/// should not be implemented by the user.
9pub trait EventSender: Sendable + 'static {
10    fn send_event(&mut self, event: Event);
11}
12
13/// An event sink, that can send events to the application.
14#[derive(Clone)]
15pub struct EventSink {
16    sender: Shared<Lock<dyn EventSender>>,
17}
18
19impl EventSink {
20    /// Creates a new event sink from an [`EventSender`].
21    pub fn new(sender: impl EventSender) -> Self {
22        Self {
23            sender: Shared::new(Lock::new(sender)),
24        }
25    }
26
27    /// Sends an event to the application.
28    pub fn send(&self, event: impl Any + SendSync) {
29        self.sender.lock_mut().send_event(Event::new(event));
30    }
31}
32
33impl Debug for EventSink {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("EventSink").finish()
36    }
37}