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
use std::{any::Any, fmt::Debug, rc::Rc};

use serde_json::Value;

use flowrs::{
    connection::{Input, Output, RuntimeConnectable},
    node::{Node, State, UpdateError, Context},
};

pub struct DebugNode<I>
where
    I: Clone,
{
    name: String,
    _state: State<Option<I>>,
    _props: Value,
    _context: State<Context>,

    pub input: Input<I>,
    pub output: Output<I>,
}

impl<I> DebugNode<I>
where
    I: Clone,
{
    pub fn new(name: &str, context: State<Context>, props: Value) -> Self {
        Self {
            name: name.into(),
            _state: State::new(None),
            _props: props,
            _context: context.clone(),
            input: Input::new(),
            output: Output::new(context.clone()),
        }
    }
}

impl<I> Node for DebugNode<I>
where
    I: Clone + Debug + Send + 'static,
{
    fn on_init(&self) {}

    fn on_ready(&self) {}

    fn on_shutdown(&self) {}

    fn name(&self) -> &str {
        &self.name
    }

    fn update(&self) -> Result<(), UpdateError> {
        if let Ok(input) = self.input.next_elem() {
            println!("{:?}", input);
            self.output.clone().send(input).unwrap();
        }
        Ok(())
    }
}

impl<I: Clone + 'static> RuntimeConnectable for DebugNode<I> {
    fn input_at(&self, index: usize) -> Rc<dyn Any> {
        match index {
            0 => Rc::new(self.input.clone()),
            _ => panic!("Intex out of bounds for DebugNode"),
        }
    }

    fn output_at(&self, index: usize) -> Rc<dyn Any> {
        match index {
            0 => Rc::new(self.output.clone()),
            _ => panic!("Intex out of bounds for DebugNode"),
        }
    }
}