pubsub_bus/
bus.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// *************************************************************************
//
// Copyright (c) 2025 Andrei Gramakov. All rights reserved.
//
// This file is licensed under the terms of the MIT license.
// For a copy, see: https://opensource.org/licenses/MIT
//
// site:    https://agramakov.me
// e-mail:  mail@agramakov.me
//
// *************************************************************************
use super::{Event, Subscriber};
use std::sync::{Arc, Mutex};

#[cfg(test)]
mod tests;

pub struct EventBus<ContentType> {
    subscribers: Vec<Arc<Mutex<dyn Subscriber<ContentType>>>>,
}

impl<ContentType> EventBus<ContentType> {
    pub fn new() -> Self {
        Self {
            subscribers: Vec::new(),
        }
    }

    pub fn subscribe(&mut self, subscriber: Arc<Mutex<dyn Subscriber<ContentType>>>) {
        self.subscribers.push(subscriber);
    }

    pub fn publish(&mut self, event: &Event<ContentType>) {
        for s in self.subscribers.iter_mut() {
            s.lock().unwrap().on_event(event);
        }
    }
}