Skip to main content

armature/
sender.rs

1use futures::channel::mpsc;
2use crate::event::*;
3
4
5/// Trait for sending events to the commutator.
6pub trait Sender {
7    type Event: IsEvent<Event = Self::Event>;
8
9    /// Get a mutable reference to the event sender component.
10    fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event>;
11
12    /// Get an immutable reference to the event sender component.
13    fn get_sender_component(&self) -> &SenderComponent<Self::Event>;
14
15    /// Set the event sender component.
16    fn set_sender_component(&mut self, sender_component: SenderComponent<Self::Event>) {
17        *self.get_sender_component_mut() = sender_component;
18    }
19    
20    /// Get the event sender
21    fn get_sender(&self) -> mpsc::UnboundedSender<Envelope<Self::Event>> {
22        self.get_sender_component().sender.as_ref().unwrap().clone()
23    }
24
25    /// Set the event sender
26    fn set_sender(&mut self, sender: mpsc::UnboundedSender<Envelope<Self::Event>>) {
27        self.get_sender_component_mut().sender = Some(sender);
28    }
29
30    /// Clear the event sender
31    fn clear_sender(&mut self) {
32        self.get_sender_component_mut().sender = None;
33    }
34
35    /// Get associated event handler id, this is the id of the event handler 
36    /// that owns the event sender.
37    fn set_associated_handler_id(&mut self, id: usize) {
38        self.get_sender_component_mut().associated_handler_id = Some(id);
39    }
40
41    /// Get the associated event handler id.
42    fn get_associated_handler_id(&self) -> Option<usize> {
43        self.get_sender_component().associated_handler_id
44    }
45
46    /// Clear the associated event handler id.
47    fn clear_associated_handler_id(&mut self) {
48        self.get_sender_component_mut().associated_handler_id = None;
49    }
50
51    /// Publish an event to all handlers.
52    fn publish(&self, event: Self::Event) {
53        let envelope = Envelope {
54            destination: Destination::All,
55            event
56        };
57        self.send(envelope);
58    }
59
60    /// Post an event to a specific handler.
61    fn post(&self, event: Self::Event, handler_id: usize) {
62        let envelope = Envelope {
63            destination: Destination::Single(handler_id),
64            event
65        };
66        self.send(envelope);
67    }
68
69    /// Post an event to event handler to which the event sender is attached.
70    fn post_to_self(&self, event: Self::Event) {
71        let id = self.get_associated_handler_id().expect(
72            "Stator must be attached to commutator");
73        self.post(event, id);
74    }
75
76    /// Send an envelope
77    fn send(&self, envelope: Envelope<Self::Event>) {
78        let sender_component = self.get_sender_component();
79        if let Some(sender) = &sender_component.sender {
80            let _ = sender.unbounded_send(envelope);
81        } else {
82            panic!("no event sender set");
83        }
84    }
85
86}
87
88#[derive(Clone, Debug)]
89pub struct SenderComponent<E>
90where
91E: IsEvent<Event = E> {
92    sender: Option<mpsc::UnboundedSender<Envelope<E>>>,
93    associated_handler_id: Option<usize>
94}
95
96impl<E> Default for SenderComponent<E>
97where 
98E: IsEvent<Event = E> {
99
100    fn default() -> Self {
101        Self {
102            sender: None,
103            associated_handler_id: None
104        }
105    }
106
107}
108
109impl<E: IsEvent<Event = E>> Sender for SenderComponent<E> {
110    type Event = E;
111
112    fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event> {
113        self
114    }
115
116    fn get_sender_component(&self) -> &SenderComponent<Self::Event> {
117        self
118    }
119
120}