flurx 0.1.6

Create reactive asynchronous data flow
Documentation
use core::marker::PhantomData;

use crate::selector::Selector;

/// Create the task continues to run as long as the state meets the condition.
#[inline]
pub fn until<F, State>(f: F) -> impl Selector<State>
    where
        F: Fn(State) -> bool,
{
    Until(f, PhantomData)
}

struct Until<F, State>(F, PhantomData<State>);

impl<F, State> Selector<State> for Until<F, State>
    where
        F: Fn(State) -> bool
{
    type Output = ();

    fn select(&self, state: State) -> Option<Self::Output> {
        self.0(state).then_some(())
    }
}


#[cfg(test)]
mod tests {
    use crate::Scheduler;
    use crate::selector::wait;
    use crate::tests::result_event;

    #[tokio::test]
    async fn until_string_is_hello() {
        let mut scheduler = Scheduler::<&'static str>::default();
        let (tx, rx) = result_event();
        scheduler.schedule(|task| async move {
            task.will(wait::until(|state: &'static str| {
                state == "hello"
            })).await;
            tx.set(true);
        });
        scheduler.run("hello").await;
        scheduler.run("hello").await;
        scheduler.run("end").await;

        assert!(rx.get());
    }
}