use std::{fmt::Debug, time::Duration};
use crate::batch::Batch;
#[derive(Debug)]
#[non_exhaustive]
pub enum BatchingStrategy {
Size(usize),
Duration(Duration),
Debounce(Duration),
Sequential,
}
pub enum BatchingResult {
Process,
ProcessAfter(Duration),
DoNothing,
}
impl BatchingStrategy {
pub(crate) fn is_sequential(&self) -> bool {
matches!(self, Self::Sequential)
}
pub(crate) fn apply<K, I, O, E>(&self, batch: &Batch<K, I, O, E>) -> BatchingResult
where
K: 'static + Send + Clone,
{
match self {
Self::Size(size) if batch.len() >= *size => BatchingResult::Process,
Self::Duration(dur) if batch.is_new_batch() => BatchingResult::ProcessAfter(*dur),
Self::Debounce(dur) => BatchingResult::ProcessAfter(*dur),
Self::Sequential if !batch.is_running() => BatchingResult::Process,
_ => BatchingResult::DoNothing,
}
}
}