logic_mesh/blocks/time/
now.rs1use crate::{
4 base::{
5 block::{Block, BlockDesc, BlockProps, BlockState},
6 input::{input_reader::InputReader, Input, InputProps},
7 output::{Output, OutputProps},
8 },
9 blocks::utils::input_to_millis_or_default,
10};
11use std::time::Duration;
12use uuid::Uuid;
13
14use crate::tokio_impl::sleep::current_time_millis;
15use crate::{blocks::InputImpl, blocks::OutputImpl};
16use libhaystack::val::kind::HaystackKind;
17
18#[block]
20#[derive(BlockProps, Debug)]
21#[category = "time"]
22pub struct Now {
23 #[input(name = "resolution", kind = "Number")]
24 pub resolution: InputImpl,
25 #[output(kind = "Number")]
26 pub out: OutputImpl,
27}
28
29impl Block for Now {
30 async fn execute(&mut self) {
31 let millis = input_to_millis_or_default(&self.resolution.val);
32
33 self.wait_on_inputs(Duration::from_millis(millis)).await;
34
35 if !self.out.is_connected() {
36 return;
37 }
38
39 self.out.set((current_time_millis() as f64).into());
40 }
41}
42
43#[cfg(test)]
44mod test {
45
46 use crate::{
47 base::{block::Block, link::BaseLink, output::Output},
48 blocks::time::Now,
49 };
50
51 #[tokio::test]
52 async fn test_now_block() {
53 let mut block = Now::new();
54 block
55 .out
56 .add_link(BaseLink::new(uuid::Uuid::new_v4(), "testIn".into()));
57
58 block.execute().await;
59 let now = block.out.value.clone();
60
61 block.execute().await;
62 assert!(block.out.value > now);
63 }
64}