#![allow(clippy::must_use_candidate)]
use std::sync::Arc;
use hm_plugin_protocol::BuildEvent;
use tokio::sync::broadcast;
const BUS_CAPACITY: usize = 1024;
#[derive(Debug, Clone)]
pub struct EventBus {
tx: broadcast::Sender<BuildEvent>,
}
impl EventBus {
pub fn new() -> Arc<Self> {
let (tx, _rx) = broadcast::channel(BUS_CAPACITY);
Arc::new(Self { tx })
}
pub fn subscribe(&self) -> broadcast::Receiver<BuildEvent> {
self.tx.subscribe()
}
pub fn emit(&self, event: BuildEvent) {
let _ = self.tx.send(event);
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
#[tokio::test]
async fn emit_and_receive() {
let bus = EventBus::new();
let mut rx = bus.subscribe();
bus.emit(BuildEvent::BuildEnd {
exit_code: 0,
duration_ms: 1,
});
let ev = rx.recv().await.unwrap();
matches!(ev, BuildEvent::BuildEnd { exit_code: 0, .. });
}
#[tokio::test]
async fn no_subscribers_is_not_an_error() {
let bus = EventBus::new();
bus.emit(BuildEvent::BuildEnd {
exit_code: 0,
duration_ms: 0,
});
}
}