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
77
78
79
80
81
82
use std::rc::Rc;

use wasm_bindgen::JsValue;

use super::{AnyNodeValue, Node};

#[derive(Debug, Clone)]
pub enum AnyNode {
    /// No node.
    /// Will be converted to `null` when passed to js
    Null,
    /// Valid `ReactNode` excluding elements.
    /// Such as number, string, boolean.
    Value(AnyNodeValue),
    /// Element
    Element(crate::Element),
    /// Array of keyed elements
    Multiple(Rc<Vec<crate::Keyed<crate::Element>>>),
}

impl AnyNode {
    #[inline]
    pub fn null() -> Self {
        Self::Null
    }

    /// Consumes `AnyNode` and output the node value
    /// so that it can be passed to js.
    ///
    /// Note that the produced JsValue shouldn't
    /// be cloned due to the following reasons:
    ///
    /// If it is an element
    /// it will drop the `use_render` closure
    /// which has been previously forgotten in
    /// [`crate::BridgeElementData::unsafe_create_element_js`].
    #[inline]
    pub(crate) fn unsafe_into_js_node_value(self) -> JsValue {
        let js = match self {
            AnyNode::Null => JsValue::NULL,
            AnyNode::Value(v) => v.into(),
            AnyNode::Element(el) => el.unsafe_into_js_element().into(),
            AnyNode::Multiple(els) => (match Rc::try_unwrap(els) {
                Ok(els) => js_sys::Array::from_iter(
                    els.into_iter()
                        .map(|node| node.0.into_node().unsafe_into_js_node_value()),
                ),
                Err(els) => js_sys::Array::from_iter(
                    els.iter()
                        .map(|node| node.0.to_node().unsafe_into_js_node_value()),
                ),
            })
            .into(),
        };
        js
    }
}

impl Node for AnyNode {
    #[inline]
    fn to_node(&self) -> AnyNode {
        self.clone()
    }

    #[inline]
    fn to_children(&self) -> Option<crate::Children> {
        Some(crate::Children::from_single(self.clone()))
    }

    #[inline]
    fn into_node(self) -> AnyNode {
        self
    }

    #[inline]
    fn into_children(self) -> Option<crate::Children>
    where
        Self: Sized,
    {
        Some(crate::Children::from_single(self))
    }
}