diff --git a/src/context.rs b/src/context.rs index f2838744..1835dd25 100644 --- a/src/context.rs +++ b/src/context.rs @@ -174,30 +174,32 @@ impl Context { } pub fn execute(&mut self) { - // Execute callbacks if there are any in the queue - loop { - let callback = self.callback_queue.pop_front(); - match callback { - Some(callback) => callback(self), - None => break, - } - } // Start plan loop loop { - let timed_plan = self.plan_queue.get_next_timed_plan(); - match timed_plan { - Some(timed_plan) => { - self.current_time = timed_plan.time; - (timed_plan.callback)(self); - loop { - let callback = self.callback_queue.pop_front(); - match callback { - Some(callback) => callback(self), - None => break, - } - } + let mut executed = false; + + // First execute callbacks. + loop { + let callback = self.callback_queue.pop_front(); + match callback { + Some(callback) => { + executed = true; + callback(self) + }, + None => break, } - None => break, + } + + // Now execute the first timed plan. + if let Some(timed_plan) = self.plan_queue.get_next_timed_plan() { + executed = true; + self.current_time = timed_plan.time; + (timed_plan.callback)(self); + } + + // If nothing happened, then we are done. + if !executed { + break; } } }