new event_man module
This commit is contained in:
parent
3d5654a5e6
commit
b8d5f75cff
@ -1,3 +1,4 @@
|
|||||||
pub mod events;
|
pub mod events;
|
||||||
|
pub mod event_man;
|
||||||
pub mod executable;
|
pub mod executable;
|
||||||
pub mod objects;
|
pub mod objects;
|
||||||
|
67
src/core/event_man.rs
Normal file
67
src/core/event_man.rs
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
|
use crate::core::events::{Event, EventRaw, GroupId};
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
||||||
|
enum ListenerType {
|
||||||
|
Single(EventRaw),
|
||||||
|
Group(GroupId),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Listener {
|
||||||
|
_ltype: ListenerType,
|
||||||
|
dest: Sender<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventManager {
|
||||||
|
listeners: HashMap<ListenerType, Vec<Listener>>,
|
||||||
|
event_receiver: Receiver<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventManager {
|
||||||
|
pub fn subcribe_single(&mut self, event: Event, dest: Sender<Event>) {
|
||||||
|
self.update_listeners(ListenerType::Single(event.raw()), dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subscribe_group(&mut self, group_id: GroupId, dest: Sender<Event>) {
|
||||||
|
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 }]);
|
||||||
|
} else {
|
||||||
|
let vec = self.listeners.get_mut(&key).unwrap();
|
||||||
|
vec.push(Listener { _ltype: key, dest });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn periodic_op(&mut self) {
|
||||||
|
if let Ok(event) = self.event_receiver.recv() {
|
||||||
|
println!("Received event {:?}", event);
|
||||||
|
for (ltype, listener_list) in self.listeners.iter() {
|
||||||
|
match ltype {
|
||||||
|
ListenerType::Single(raw_event) => {
|
||||||
|
if event.raw() == *raw_event {
|
||||||
|
for listener in listener_list {
|
||||||
|
listener.dest.send(event).expect("Sending event failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ListenerType::Group(group_id) => {
|
||||||
|
if event.group_id() == *group_id {
|
||||||
|
for listener in listener_list {
|
||||||
|
listener.dest.send(event).expect("Sending event failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
}
|
59
src/main.rs
59
src/main.rs
@ -1,63 +1,8 @@
|
|||||||
use launchpad::core::events::{Event, EventRaw, GroupId, Severity};
|
use launchpad::core::events::{Event, Severity};
|
||||||
use launchpad::core::executable::{Executable, ExecutionType, OpResult};
|
use std::sync::mpsc::channel;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
|
||||||
enum ListenerType {
|
|
||||||
SINGLE(EventRaw),
|
|
||||||
GROUP(GroupId),
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Listener {
|
|
||||||
_ltype: ListenerType,
|
|
||||||
dest: Sender<Event>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct EventManager {
|
|
||||||
listeners: HashMap<ListenerType, Vec<Listener>>,
|
|
||||||
event_receiver: Receiver<Event>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Executable for EventManager {
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn exec_type(&self) -> ExecutionType {
|
|
||||||
ExecutionType::Infinite
|
|
||||||
}
|
|
||||||
|
|
||||||
fn task_name(&self) -> &'static str {
|
|
||||||
"Event Manager"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn periodic_op(&mut self, _op_code: i32) -> Result<OpResult, Self::Error> {
|
|
||||||
if let Ok(event) = self.event_receiver.recv() {
|
|
||||||
println!("Received event {:?}", event);
|
|
||||||
}
|
|
||||||
Ok(OpResult::Ok)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventManager {
|
|
||||||
pub fn subcribe_single(&mut self, event: Event, dest: Sender<Event>) {
|
|
||||||
self.update_listeners(ListenerType::SINGLE(event.raw()), dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscribe_group(&mut self, group_id: GroupId, dest: Sender<Event>) {
|
|
||||||
self.update_listeners(ListenerType::GROUP(group_id), dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_listeners(&mut self, key: ListenerType, dest: Sender<Event>) {
|
|
||||||
if !self.listeners.contains_key(&key) {
|
|
||||||
self.listeners
|
|
||||||
.insert(key.clone(), vec![Listener { _ltype: key, dest }]);
|
|
||||||
} else {
|
|
||||||
let vec = self.listeners.get_mut(&key).unwrap();
|
|
||||||
vec.push(Listener { _ltype: key, dest });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn main() {
|
fn main() {
|
||||||
basic_event_test();
|
basic_event_test();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user