Init Event Module #4

Merged
muellerr merged 16 commits from events into main 2022-06-11 12:36:40 +02:00
Showing only changes of commit 3bd8c58340 - Show all commits

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::mpsc::{Receiver, Sender};
use crate::core::events::{Event, EventRaw, GroupId}; use crate::core::events::{Event, EventRaw, GroupId};
#[derive(PartialEq, Eq, Hash, Copy, Clone)] #[derive(PartialEq, Eq, Hash, Copy, Clone)]
@ -8,60 +7,74 @@ enum ListenerType {
Group(GroupId), Group(GroupId),
} }
struct Listener { pub trait EventRecipient {
type Error;
fn send_to(&mut self, event: Event) -> Result<(), Self::Error>;
}
struct Listener<E> {
_ltype: ListenerType, _ltype: ListenerType,
dest: Sender<Event>, dest: Box<dyn EventRecipient<Error=E>>,
} }
pub struct EventManager { pub trait ReceivesEvent {
listeners: HashMap<ListenerType, Vec<Listener>>, fn receive(&mut self) -> Option<Event>;
event_receiver: Receiver<Event>,
} }
impl EventManager {
pub fn subcribe_single(&mut self, event: Event, dest: Sender<Event>) { pub struct EventManager<E> {
listeners: HashMap<ListenerType, Vec<Listener<E>>>,
event_receiver: dyn ReceivesEvent,
}
impl<E> EventManager<E> {
pub fn subcribe_single(&mut self, event: Event, dest: impl EventRecipient<Error=E> + 'static) {
self.update_listeners(ListenerType::Single(event.raw()), dest); self.update_listeners(ListenerType::Single(event.raw()), dest);
} }
pub fn subscribe_group(&mut self, group_id: GroupId, dest: Sender<Event>) { pub fn subscribe_group(&mut self, group_id: GroupId, dest: impl EventRecipient<Error=E> + 'static) {
self.update_listeners(ListenerType::Group(group_id), dest); self.update_listeners(ListenerType::Group(group_id), dest);
} }
fn update_listeners(&mut self, key: ListenerType, dest: Sender<Event>) { fn update_listeners (&mut self, key: ListenerType, dest: impl EventRecipient<Error=E> + 'static) {
if let std::collections::hash_map::Entry::Vacant(e) = self.listeners.entry(key) { if !self.listeners.contains_key(&key) {
e.insert(vec![Listener { _ltype: key, dest }]); self.listeners.insert(key, vec![Listener { _ltype: key, dest: Box::new(dest) }]);
} else { } else {
let vec = self.listeners.get_mut(&key).unwrap(); let vec = self.listeners.get_mut(&key).unwrap();
vec.push(Listener { _ltype: key, dest }); vec.push(Listener { _ltype: key, dest: Box::new(dest) });
} }
} }
pub fn periodic_op(&mut self) { pub fn periodic_op(&mut self) -> Result<(), E> {
if let Ok(event) = self.event_receiver.recv() { if let Some(event) = self.event_receiver.receive() {
println!("Received event {:?}", event); println!("Received event {:?}", event);
for (ltype, listener_list) in self.listeners.iter() { for (ltype, listener_list) in self.listeners.iter_mut() {
match ltype { match ltype {
ListenerType::Single(raw_event) => { ListenerType::Single(raw_event) => {
if event.raw() == *raw_event { if event.raw() == *raw_event {
for listener in listener_list { for listener in listener_list.iter_mut() {
listener.dest.send(event).expect("Sending event failed"); listener.dest.send_to(event)?;
} }
} }
} }
ListenerType::Group(group_id) => { ListenerType::Group(group_id) => {
if event.group_id() == *group_id { if event.group_id() == *group_id {
for listener in listener_list { for listener in listener_list.iter_mut() {
listener.dest.send(event).expect("Sending event failed"); listener.dest.send_to(event)?;
} }
} }
} }
} }
} }
} }
Ok(())
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test]
fn basic_test() {
} }
}