kind of awkward, but maybe there is no other way..

This commit is contained in:
Robin Müller 2022-06-10 18:47:56 +02:00
parent b8d5f75cff
commit 3bd8c58340
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC

View File

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