//! # Next run
use atap::{RuntimeError, TaskHandle};
use std::{
thread,
time::{Duration, Instant},
};
/// Takes the next output a repeat produces
///
/// ## Returns
/// `None` once the series has ended, or once `patience` has
/// run out
pub fn next_run<T>(handle: &TaskHandle<T>, patience: Duration) -> Option<T> {
let deadline = Instant::now() + patience;
while Instant::now() < deadline {
match handle.try_take() {
Ok(value) => return Some(value),
// Between runs, or one still going
Err(RuntimeError::AlreadyTaken) | Err(RuntimeError::NotReady) => {
thread::sleep(Duration::from_millis(1))
}
// `Finished` and every other error are endings
Err(_) => break,
}
}
None
}