huskies: merge 858
This commit is contained in:
@@ -21,21 +21,25 @@ pub trait TransitionSubscriber: Send + Sync {
|
||||
fn on_transition(&self, fired: &TransitionFired);
|
||||
}
|
||||
|
||||
/// Collects [`TransitionSubscriber`]s and dispatches [`TransitionFired`] events to each.
|
||||
pub struct EventBus {
|
||||
subscribers: Vec<Box<dyn TransitionSubscriber>>,
|
||||
}
|
||||
|
||||
impl EventBus {
|
||||
/// Create an empty event bus with no subscribers.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
subscribers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a subscriber to receive all future transition events.
|
||||
pub fn subscribe<S: TransitionSubscriber + 'static>(&mut self, subscriber: S) {
|
||||
self.subscribers.push(Box::new(subscriber));
|
||||
}
|
||||
|
||||
/// Fire a transition event, calling every registered subscriber in order.
|
||||
pub fn fire(&self, event: TransitionFired) {
|
||||
for sub in &self.subscribers {
|
||||
sub.on_transition(&event);
|
||||
|
||||
Reference in New Issue
Block a user