1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use futures::channel::mpsc;
use crate::event::*;


/// Trait for sending events to the commutator.
pub trait Sender {
    type Event: IsEvent<Event = Self::Event>;

    /// Get a mutable reference to the event sender component.
    fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event>;

    /// Get an immutable reference to the event sender component.
    fn get_sender_component(&self) -> &SenderComponent<Self::Event>;

    /// Set the event sender component.
    fn set_sender_component(&mut self, sender_component: SenderComponent<Self::Event>) {
        *self.get_sender_component_mut() = sender_component;
    }
    
    /// Get the event sender
    fn get_sender(&self) -> mpsc::UnboundedSender<Envelope<Self::Event>> {
        self.get_sender_component().sender.as_ref().unwrap().clone()
    }

    /// Set the event sender
    fn set_sender(&mut self, sender: mpsc::UnboundedSender<Envelope<Self::Event>>) {
        self.get_sender_component_mut().sender = Some(sender);
    }

    /// Clear the event sender
    fn clear_sender(&mut self) {
        self.get_sender_component_mut().sender = None;
    }

    /// Get associated event handler id, this is the id of the event handler 
    /// that owns the event sender.
    fn set_associated_handler_id(&mut self, id: usize) {
        self.get_sender_component_mut().associated_handler_id = Some(id);
    }

    /// Get the associated event handler id.
    fn get_associated_handler_id(&self) -> Option<usize> {
        self.get_sender_component().associated_handler_id
    }

    /// Clear the associated event handler id.
    fn clear_associated_handler_id(&mut self) {
        self.get_sender_component_mut().associated_handler_id = None;
    }

    /// Publish an event to all handlers.
    fn publish(&self, event: Self::Event) {
        let envelope = Envelope {
            destination: Destination::All,
            event
        };
        self.send(envelope);
    }

    /// Post an event to a specific handler.
    fn post(&self, event: Self::Event, handler_id: usize) {
        let envelope = Envelope {
            destination: Destination::Single(handler_id),
            event
        };
        self.send(envelope);
    }

    /// Post an event to event handler to which the event sender is attached.
    fn post_to_self(&self, event: Self::Event) {
        let id = self.get_associated_handler_id().expect(
            "Stator must be attached to commutator");
        self.post(event, id);
    }

    /// Send an envelope
    fn send(&self, envelope: Envelope<Self::Event>) {
        let sender_component = self.get_sender_component();
        if let Some(sender) = &sender_component.sender {
            let _ = sender.unbounded_send(envelope);
        } else {
            panic!("no event sender set");
        }
    }

}

#[derive(Clone, Debug)]
pub struct SenderComponent<E>
where
E: IsEvent<Event = E> {
    sender: Option<mpsc::UnboundedSender<Envelope<E>>>,
    associated_handler_id: Option<usize>
}

impl<E> Default for SenderComponent<E>
where 
E: IsEvent<Event = E> {

    fn default() -> Self {
        Self {
            sender: None,
            associated_handler_id: None
        }
    }

}

impl<E: IsEvent<Event = E>> Sender for SenderComponent<E> {
    type Event = E;

    fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event> {
        self
    }

    fn get_sender_component(&self) -> &SenderComponent<Self::Event> {
        self
    }

}