2022-06-10 18:10:40 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use crate::core::events::{Event, EventRaw, GroupId};
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
|
|
|
enum ListenerType {
|
|
|
|
Single(EventRaw),
|
|
|
|
Group(GroupId),
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
pub trait EventRecipient {
|
|
|
|
type Error;
|
|
|
|
fn send_to(&mut self, event: Event) -> Result<(), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Listener<E> {
|
2022-06-10 18:10:40 +02:00
|
|
|
_ltype: ListenerType,
|
2022-06-10 18:47:56 +02:00
|
|
|
dest: Box<dyn EventRecipient<Error=E>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ReceivesEvent {
|
|
|
|
fn receive(&mut self) -> Option<Event>;
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
|
|
|
|
pub struct EventManager<E> {
|
|
|
|
listeners: HashMap<ListenerType, Vec<Listener<E>>>,
|
|
|
|
event_receiver: dyn ReceivesEvent,
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
impl<E> EventManager<E> {
|
|
|
|
pub fn subcribe_single(&mut self, event: Event, dest: impl EventRecipient<Error=E> + 'static) {
|
2022-06-10 18:10:40 +02:00
|
|
|
self.update_listeners(ListenerType::Single(event.raw()), dest);
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
pub fn subscribe_group(&mut self, group_id: GroupId, dest: impl EventRecipient<Error=E> + 'static) {
|
2022-06-10 18:10:40 +02:00
|
|
|
self.update_listeners(ListenerType::Group(group_id), dest);
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
fn update_listeners (&mut self, key: ListenerType, dest: impl EventRecipient<Error=E> + 'static) {
|
2022-06-10 18:54:30 +02:00
|
|
|
if let std::collections::hash_map::Entry::Vacant(e) = self.listeners.entry(key) {
|
|
|
|
e.insert(vec![Listener { _ltype: key, dest: Box::new(dest) }]);
|
2022-06-10 18:10:40 +02:00
|
|
|
} else {
|
|
|
|
let vec = self.listeners.get_mut(&key).unwrap();
|
2022-06-10 18:47:56 +02:00
|
|
|
vec.push(Listener { _ltype: key, dest: Box::new(dest) });
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:54:30 +02:00
|
|
|
pub fn handle_one_event(&mut self) -> Result<(), E> {
|
|
|
|
let mut status = Ok(());
|
2022-06-10 18:47:56 +02:00
|
|
|
if let Some(event) = self.event_receiver.receive() {
|
|
|
|
for (ltype, listener_list) in self.listeners.iter_mut() {
|
2022-06-10 18:10:40 +02:00
|
|
|
match ltype {
|
|
|
|
ListenerType::Single(raw_event) => {
|
|
|
|
if event.raw() == *raw_event {
|
2022-06-10 18:47:56 +02:00
|
|
|
for listener in listener_list.iter_mut() {
|
2022-06-10 18:54:30 +02:00
|
|
|
if let Err(e) = listener.dest.send_to(event) {
|
|
|
|
status = Err(e);
|
|
|
|
}
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ListenerType::Group(group_id) => {
|
|
|
|
if event.group_id() == *group_id {
|
2022-06-10 18:47:56 +02:00
|
|
|
for listener in listener_list.iter_mut() {
|
2022-06-10 18:54:30 +02:00
|
|
|
if let Err(e) = listener.dest.send_to(event) {
|
|
|
|
status = Err(e);
|
|
|
|
}
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 18:54:30 +02:00
|
|
|
status
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-06-10 18:47:56 +02:00
|
|
|
#[test]
|
|
|
|
fn basic_test() {
|
2022-06-10 18:10:40 +02:00
|
|
|
|
2022-06-10 18:47:56 +02:00
|
|
|
}
|
2022-06-10 18:10:40 +02:00
|
|
|
}
|