1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
use std::time::Duration;
/// By implementing the `TestClock` trait, [`TestStore`] can be used to test
/// `Reducer`s that utilize [tasks], [futures], or [streams].
///
/// This [`debounce`] example exercises scheduling and task cancellation; all
/// with deterministic control of over the (simulated) passage of time.
///
/// [`TestStore`]: `crate::TestStore`
/// [`debounce`]: `crate::effects::Scheduler::debounce`
///
/// [tasks]: `crate::effects::Scheduler`
/// [futures]: `crate::effects::Effects::future`
/// [streams]: `crate::effects::Effects::stream`
///
/// ```rust
/// # use std::time::Duration;
/// # use composable::*;
/// #
/// #[derive(Debug, Default)]
/// struct State {
/// previous: Option<Task>,
/// n: usize,
/// }
///
/// # impl PartialEq for State {
/// # fn eq(&self, other: &Self) -> bool {
/// # self.n.eq(&other.n)
/// # }
/// # }
/// #
/// # impl Clone for State {
/// # fn clone(&self) -> Self {
/// # Self {
/// # previous: None,
/// # n: self.n
/// # }
/// # }
/// # }
/// #[derive(Clone, Debug, PartialEq)]
/// enum Action {
/// Send,
/// Recv,
/// }
///
/// use Action::*;
///
/// impl Reducer for State {
/// type Action = Action;
/// type Output = Self;
///
/// fn reduce(&mut self, action: Action, send: impl Effects<Action>) {
/// match action {
/// Send => {
/// send.debounce(
/// Recv,
/// &mut self.previous,
/// Interval::Trailing(Duration::from_secs(4)),
/// );
/// }
/// Recv => {
/// self.n += 1;
/// }
/// }
/// }
/// }
///
/// let mut store = TestStore::<State>::default();
/// let no_change: fn(&mut State) = |State| {};
///
/// store.send(Send, no_change);
/// store.advance(Duration::from_secs(3));
///
/// store.send(Send, no_change);
/// store.advance(Duration::from_secs(8));
/// store.recv(Recv, |state| state.n = 1);
///
/// store.send(Send, no_change);
/// store.advance(Duration::from_secs(1));
/// store.advance(Duration::from_secs(1));
/// store.advance(Duration::from_secs(1));
/// store.advance(Duration::from_secs(1));
/// store.recv(Recv, |state| state.n = 2);
/// ```
pub trait TestClock {
fn advance(&mut self, duration: Duration);
}