use super::super::DebouncePolicy;
use super::stream::PollingStream;
use crate::network::AddressFetcher;
use crate::time::{Clock, SystemClock};
use std::time::Duration;
pub struct PollingMonitor<F, C = SystemClock> {
fetcher: F,
clock: C,
interval: Duration,
debounce: Option<DebouncePolicy>,
}
impl<F> PollingMonitor<F, SystemClock>
where
F: AddressFetcher,
{
#[must_use]
pub const fn new(fetcher: F, interval: Duration) -> Self {
Self::with_clock(fetcher, SystemClock, interval)
}
}
impl<F, C> PollingMonitor<F, C>
where
F: AddressFetcher,
C: Clock,
{
#[must_use]
pub const fn with_clock(fetcher: F, clock: C, interval: Duration) -> Self {
Self {
fetcher,
clock,
interval,
debounce: None,
}
}
#[must_use]
pub const fn with_debounce(mut self, policy: DebouncePolicy) -> Self {
self.debounce = Some(policy);
self
}
#[must_use]
pub const fn interval(&self) -> Duration {
self.interval
}
#[must_use]
pub const fn debounce(&self) -> Option<&DebouncePolicy> {
self.debounce.as_ref()
}
#[must_use]
pub fn into_stream(self) -> PollingStream<F, C> {
PollingStream::new(self.fetcher, self.clock, self.interval, self.debounce)
}
}