flarrow_builtins/
timer.rs

1use std::time::Duration;
2
3use crate::prelude::*;
4
5/// Simple source node that emits a "tick" message at a specified frequency.
6#[derive(Node)]
7pub struct Timer {
8    pub output: Output<String>,
9    pub frequency: f64,
10}
11
12#[node(runtime = "default_runtime")]
13impl Node for Timer {
14    async fn new(
15        _: Inputs,
16        mut outputs: Outputs,
17        _: Queries,
18        _: Queryables,
19        configuration: serde_yml::Value,
20    ) -> Result<Self> {
21        let frequency = match configuration.get("frequency") {
22            Some(serde_yml::Value::Number(number)) => number.as_f64().unwrap_or(1.0),
23            _ => 1.0,
24        };
25
26        Ok(Self {
27            output: outputs.with("out").await?,
28            frequency,
29        })
30    }
31
32    async fn start(self: Box<Self>) -> Result<()> {
33        loop {
34            self.output
35                .send("tick".to_string())
36                .await
37                .wrap_err("Failed to send message")?;
38
39            tokio::time::sleep(Duration::from_millis((1000.0 / self.frequency) as u64)).await;
40        }
41    }
42}