events: implement a first attempt at an eventing system
This commit is contained in:
14
src/task.rs
14
src/task.rs
@@ -1,7 +1,9 @@
|
||||
use core::fmt;
|
||||
|
||||
use crate::events::{Event, EventBus};
|
||||
|
||||
pub trait Task: Send {
|
||||
fn tick(&mut self) {}
|
||||
fn tick(&mut self, event: &Event, bus: &mut EventBus) {}
|
||||
fn start(&mut self) {}
|
||||
fn stop(&mut self) {}
|
||||
fn name(&self) -> &'static str {
|
||||
@@ -55,7 +57,7 @@ impl ScheduledTask {
|
||||
}
|
||||
}
|
||||
|
||||
fn tick(&mut self) {
|
||||
fn tick(&mut self, event: &Event, bus: &mut EventBus) {
|
||||
self.state = match self.state {
|
||||
ScheduledState::Start => {
|
||||
log::info!("Starting task {}", self.task.name());
|
||||
@@ -63,7 +65,7 @@ impl ScheduledTask {
|
||||
ScheduledState::Running
|
||||
},
|
||||
ScheduledState::Running => {
|
||||
self.task.tick();
|
||||
self.task.tick(event, bus);
|
||||
ScheduledState::Running
|
||||
},
|
||||
ScheduledState::Stop => {
|
||||
@@ -96,10 +98,10 @@ impl<const TASK_COUNT: usize> FixedSizeScheduler<TASK_COUNT> {
|
||||
}
|
||||
|
||||
impl<const TASK_COUNT: usize> Scheduler for FixedSizeScheduler<TASK_COUNT> {
|
||||
fn tick(&mut self) {
|
||||
fn tick(&mut self, event: &Event, bus: &mut EventBus) {
|
||||
for slot in &mut self.tasks {
|
||||
match slot {
|
||||
Some(task) => task.tick(),
|
||||
Some(task) => task.tick(event, bus),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
@@ -107,5 +109,5 @@ impl<const TASK_COUNT: usize> Scheduler for FixedSizeScheduler<TASK_COUNT> {
|
||||
}
|
||||
|
||||
pub trait Scheduler {
|
||||
fn tick(&mut self);
|
||||
fn tick(&mut self, event: &Event, bus: &mut EventBus);
|
||||
}
|
Reference in New Issue
Block a user