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

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

use flowrs::node::Context;

pub struct BasicNode<I>
where
    I: Clone,
{
    name: String,
    _state: State<Option<I>>,
    props: I,
    _context: State<Context>,

    pub output: Output<I>,
}

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

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

    fn on_ready(&self) {
        let elem = &self.props;
        self.output.clone().send(elem.clone()).unwrap();
    }

    fn on_shutdown(&self) {}

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

    fn update(&self) -> Result<(), UpdateError> {
        Ok(())
    }
}

impl<I: Clone + 'static> RuntimeConnectable for BasicNode<I> {
    fn input_at(&self, _: usize) -> Rc<dyn Any> {
        panic!("Index out of bounds for BasicNode")
    }

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