orchestrator
This commit is contained in:
46
src/orchestrator/event_bus.rs
Normal file
46
src/orchestrator/event_bus.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
// path_from_the_root: src/orchestrator/event_bus.rs
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub trait EventHandler<E> {
|
||||
fn handle(&mut self, event: E);
|
||||
}
|
||||
|
||||
pub struct EventBus<E: Clone> {
|
||||
handlers: Vec<Box<dyn EventHandler<E>>>,
|
||||
}
|
||||
|
||||
impl<E: Clone> Default for EventBus<E> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Clone> EventBus<E> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
handlers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, handler: Box<dyn EventHandler<E>>) {
|
||||
self.handlers.push(handler);
|
||||
}
|
||||
|
||||
pub fn emit(&mut self, event: E) {
|
||||
for handler in &mut self.handlers {
|
||||
handler.handle(event.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handler_count(&self) -> usize {
|
||||
self.handlers.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.handlers.is_empty()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user