use nostr::types::RelayUrl;
use tokio::sync::broadcast::{self, Receiver, Sender};
use crate::relay::RelayStatus;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MonitorNotification {
StatusChanged {
relay_url: RelayUrl,
status: RelayStatus,
},
}
#[derive(Debug, Clone)]
pub struct Monitor {
channel: Sender<MonitorNotification>,
}
impl Monitor {
pub fn new(channel_size: usize) -> Self {
let (tx, ..) = broadcast::channel(channel_size);
Self { channel: tx }
}
#[inline]
pub fn subscribe(&self) -> Receiver<MonitorNotification> {
self.channel.subscribe()
}
#[inline]
fn notify(&self, notification: MonitorNotification) {
let _ = self.channel.send(notification);
}
#[inline]
pub(crate) fn notify_status_change(&self, relay_url: RelayUrl, status: RelayStatus) {
self.notify(MonitorNotification::StatusChanged { relay_url, status });
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn test_monitor_capacity_is_zero() {
Monitor::new(0);
}
#[test]
#[should_panic]
fn test_monitor_capacity_overflows() {
let _ = Monitor::new(usize::MAX / 2);
}
}