use crate::context::Receiver;
use crate::messages::Message;
use std::time::Duration;
pub struct StreamBuilder<T: Message> {
pub(super) receiver: Receiver<T>,
#[allow(clippy::type_complexity)]
pub(super) filter_fn: Option<Box<dyn Fn(&T) -> bool + Send + Sync>>,
#[allow(clippy::type_complexity)]
pub(super) map_fn: Option<Box<dyn Fn(T) -> T + Send + Sync>>,
pub(super) throttle_duration: Option<Duration>,
pub(super) debounce_duration: Option<Duration>,
pub(super) latest_only: bool,
#[allow(dead_code)]
pub(super) batch_size: Option<usize>,
#[allow(dead_code)]
pub(super) batch_timeout: Option<Duration>,
}
impl<T: Message + Send + 'static> StreamBuilder<T> {
pub fn new(receiver: Receiver<T>) -> Self {
Self {
receiver,
filter_fn: None,
map_fn: None,
throttle_duration: None,
debounce_duration: None,
latest_only: false,
batch_size: None,
batch_timeout: None,
}
}
pub fn filter<F>(mut self, predicate: F) -> Self
where
F: Fn(&T) -> bool + Send + Sync + 'static,
{
self.filter_fn = Some(Box::new(predicate));
self
}
crate::builder_setter!(
into_some, throttle, throttle_duration, Duration
);
pub fn latest(mut self) -> Self {
self.latest_only = true;
self
}
pub fn map<F>(mut self, mapper: F) -> Self
where
F: Fn(T) -> T + Send + Sync + 'static,
{
self.map_fn = Some(Box::new(mapper));
self
}
crate::builder_setter!(
into_some, debounce, debounce_duration, Duration
);
}