use floxide::{workflow, WorkflowCtx};
use floxide::node;
use floxide::{error::FloxideError, source, transition::Transition, Node};
use tokio::time::{sleep, Duration};
node! {
pub struct DoubleNode {};
context = ();
input = u64;
output = u64;
|ctx, x| {
println!("DoubleNode: ctx = {:?}", ctx);
println!("DoubleNode: input = {}", x);
Ok(Transition::Next(x * 2))
}
}
node! {
pub struct PrintNode {};
context = ();
input = u64;
output = ();
|ctx, x| {
println!("PrintNode: ctx = {:?}", ctx);
println!("PrintNode: input = {}", x);
Ok(Transition::Next(()))
}
}
workflow! {
pub struct PrintDouble {
doubler: DoubleNode,
printer: PrintNode,
}
context = ();
start = doubler;
edges {
doubler => {[printer]};
printer => {};
}
}
pub async fn run_timer_example() -> Result<(), FloxideError> {
let (tx, source) = source::<(), u64>(10);
tokio::spawn(async move {
for i in 0u64..10u64 {
sleep(Duration::from_millis(200)).await;
if tx.send(i).await.is_err() {
break;
}
}
});
let ctx = WorkflowCtx::new(());
let wf = PrintDouble {
doubler: DoubleNode {},
printer: PrintNode {},
};
source.run(&wf, &ctx).await?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), FloxideError> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
run_timer_example().await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_timer_example() {
run_timer_example()
.await
.expect("timer workflow should run");
}
}