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;

use super::super::{Instance, Props};
use super::{Children, View};

pub trait Component: 'static + Any {
    fn render(&self, instance: &Instance, props: &Props, children: &Children) -> View;

    #[inline(always)]
    fn name(&self) -> &'static str {
        "Unknown"
    }

    /// return the inital state of the component
    #[inline]
    fn initial_state(&self, _props: &Props) -> Props {
        Props::new()
    }

    /// return the inital context of the component, gets passed to children via inherit_context
    #[inline]
    fn context(&self, _props: &Props) -> Props {
        Props::new()
    }
    /// filter the passed context of the component
    #[inline]
    fn inherit_context(&self, context: Props, _parent_context: &Props) -> Props {
        context
    }

    /// called before mount
    #[inline(always)]
    fn will_mount(&self, _instance: &Instance) {}

    /// called before unmount
    #[inline(always)]
    fn will_unmount(&self, _instance: &Instance) {}

    /// called before update
    #[inline(always)]
    fn will_update(&self, _instance: &Instance) {}

    /// called when component receives new state, props, or children
    #[inline(always)]
    fn receive_props(
        &self,
        _instance: &Instance,
        _next_state: &Props,
        _next_props: &Props,
        _next_children: &Children,
    ) {
    }

    /// if component needs update return true, defaults to true
    #[inline]
    fn should_update(
        &self,
        _prev_state: &Props,
        _prev_props: &Props,
        _prev_children: &Children,

        _next_state: &Props,
        _next_props: &Props,
        _next_children: &Children,
    ) -> bool {
        true
    }
}

impl PartialEq for Component {
    #[inline]
    fn eq(&self, other: &Component) -> bool {
        self.get_type_id() == other.get_type_id()
    }
}