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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#[cfg(test)]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

mod application;
mod component;
mod dom;
mod events;
mod fetch;
mod macros;
mod routing;
mod utils;

pub use application::Application;
pub use component::{
    update_component, Checklist, ChildComp, Comp, Component, ShouldRender, WithParentComp,
};
pub use dom::attribute_types::*;
#[cfg(feature = "keyed-list")]
pub use dom::Keyed;
pub use dom::{
    HtmlUpdater as Element, ListElementCreation, ListItemRender, Nodes, RawWrapper, Render,
    StaticNodes, StaticRender,
};
#[cfg(feature = "svg")]
pub use dom::{SvgListItemRender, SvgNodes, SvgRender, SvgStaticNodes, SvgUpdater as Svg};
// TODO selectively export event traits only?
pub use events::*;
pub use fetch::{FetchError, ResponsedError};
pub use routing::{Router, Routes};
pub use utils::*;

pub use http;
pub use web_sys;

pub use wasm_bindgen::JsValue;
pub use wasm_bindgen_futures::JsFuture;

pub mod prelude {
    pub use crate::application::Application;
    pub use crate::component::Component;
    pub use crate::dom::{AttributeSetter, DomBuilder, EventSetter};
    #[cfg(feature = "svg")]
    pub use crate::dom::{SvgAttributeSetter, SvgBuilder};
    pub use crate::fetch::{FetchOptionsSetter, RawDataMode};
    pub use crate::routing::Routes;
    pub use wasm_bindgen;
    pub use wasm_bindgen::prelude::*;
    pub use wasm_bindgen::{JsCast, UnwrapThrowExt};
}

#[must_use = "This value must be returned to the framework. Otherwise, the command will be lost"]
pub struct Command<C>(Box<dyn component::Command<C>>);
#[must_use = "This value must be returned to the framework. Otherwise, the command will be lost"]
pub struct OptionCommand<C>(Option<Box<dyn component::Command<C>>>);

impl<C> From<Command<C>> for OptionCommand<C> {
    fn from(cmd: Command<C>) -> Self {
        OptionCommand(Some(cmd.0))
    }
}

impl<C> From<Command<C>> for Checklist<C>
where
    C: 'static + Component,
{
    fn from(cmd: Command<C>) -> Self {
        let mut checklist = C::default_checklist();
        checklist.add_command(cmd);
        checklist
    }
}

impl<C> From<OptionCommand<C>> for Checklist<C>
where
    C: 'static + Component,
{
    fn from(cmd: OptionCommand<C>) -> Self {
        let mut checklist = C::default_checklist();
        checklist.add_option_command(cmd);
        checklist
    }
}

impl<C> From<Option<Command<C>>> for OptionCommand<C> {
    fn from(cmd: Option<Command<C>>) -> Self {
        OptionCommand(cmd.map(|cmd| cmd.0))
    }
}

pub struct WsRef<T>(std::cell::RefCell<Option<T>>);

impl<T: wasm_bindgen::JsCast> Default for WsRef<T> {
    fn default() -> Self {
        Self::none()
    }
}

impl<T: wasm_bindgen::JsCast> WsRef<T> {
    pub fn none() -> Self {
        Self(std::cell::RefCell::new(None))
    }

    pub fn get(&self) -> std::cell::Ref<Option<T>> {
        self.0.borrow()
    }

    pub fn set<C: component::Component>(&self, element: &crate::dom::HtmlUpdater<C>) {
        use wasm_bindgen::JsCast;
        *self.0.borrow_mut() = Some(element.ws_element_clone().unchecked_into());
    }

    pub fn execute(&self, f: impl FnOnce(&T)) {
        if let Some(t) = self.get().as_ref() {
            f(t);
        }
    }
}