Commit 907836c4 authored by Ralf's avatar Ralf
Browse files

add a version of handle_events that does not take a timeout

parent 8fb5e5cd
use std::time::Duration;
use chan::{Receiver, Sender};
use chan::{Receiver, Sender, async};
use util::timeout_chan;
use input::Event;
......@@ -21,13 +21,33 @@ pub struct Environment {
}
macro_rules! handle_events {
( $env:expr, $( $x:expr ),* ) => {
{ $env.handle_events(vec![$( $x, )*] ) }
};
}
macro_rules! handle_events_timeout {
( $env:expr, $d:expr, $( $x:expr ),* ) => {
{ $env.handle_events($d, vec![$( $x, )*] ) }
{ $env.handle_events_timeout($d, vec![$( $x, )*] ) }
};
}
impl Environment {
pub fn handle_events(&mut self,
mut handlers: Vec<EventHandler>)
-> EventsResult {
for ev in self.events.clone() {
match self.handle_event(&mut handlers, ev) {
Some(result) => return result,
_ => {},
}
}
return EventsResult::ShuttingDown;
}
pub fn handle_events_timeout(&mut self,
d: Duration,
mut handlers: Vec<EventHandler>)
-> EventsResult {
......@@ -40,12 +60,9 @@ impl Environment {
match ev {
None => return EventsResult::ShuttingDown,
Some(ev) => {
self.record_state(&ev);
for handler in handlers.iter_mut() {
match handler.run(ev) {
HandlerResult::Continue => {},
HandlerResult::QuitLoop => return EventsResult::Quit,
}
match self.handle_event(&mut handlers, ev) {
Some(result) => return result,
_ => {},
}
}
}
......@@ -55,6 +72,17 @@ impl Environment {
unreachable!()
}
fn handle_event(&mut self, handlers: &mut Vec<EventHandler>, ev: Event) -> Option<EventsResult> {
self.record_state(&ev);
for handler in handlers.iter_mut() {
match handler.run(ev) {
HandlerResult::Continue => {},
HandlerResult::QuitLoop => return Some(EventsResult::Quit),
}
}
return None;
}
fn record_state(&mut self, ev: &Event) {
match ev {
&Event::DoorLocked(b) =>
......
......@@ -48,7 +48,7 @@ impl State for UnLockingState {
lock_actor.act(LockAction::Release);
lock_actor
});
let r = handle_events!(env, Duration::from_secs(1),
let r = handle_events_timeout!(env, Duration::from_secs(1),
EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| {
match ev {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment