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
//! # Example

//!

//! ```

//! extern crate kagura;

//! extern crate wasm_bindgen;

//!

//! use wasm_bindgen::prelude::*;

//!

//! use hello_component::HelloComponent;

//!

//! #[wasm_bindgen(start)]

//! pub fn main() {

//!     kagura::run::<HelloComponent, _, _, _>("app", hello_component::Props {}, vec![]);

//! }

//!

//! mod hello_component {

//!     use kagura::prelude::*;

//!

//!     pub struct Props {}

//!     pub enum Msg {}

//!     pub enum Sub {}

//!

//!     pub struct HelloComponent {}

//!

//!     impl Constructor for HelloComponent {

//!         fn constructor(_: Self::Props, _: &mut ComponentBuilder<Msg, Sub>) -> Self {

//!             Self {}

//!         }

//!     }

//!

//!     impl Component for HelloComponent {

//!         type Props = Props;

//!         type Msg = Msg;

//!         type Sub = Sub;

//!

//!         fn init(&mut self, _: Self::Props, _: &mut ComponentBuilder<Msg, Sub>) {}

//!

//!         fn update(&mut self, _: Self::Msg) -> Cmd<Msg, Sub> {

//!             Cmd::none()

//!         }

//!

//!         fn render(&self, _: Vec<Html>) -> Html {

//!             Html::h1(

//!                 Attributes::new(),

//!                 Events::new(),

//!                 vec![Html::text("Hello Kagura")],

//!             )

//!         }

//!     }

//! }

//! ```


extern crate js_sys;
extern crate rand;
extern crate serde_derive;
extern crate wasm_bindgen;
extern crate wasm_bindgen_futures;
extern crate web_sys;

pub mod component;
mod dom;
mod event;
mod native;
mod state;
mod task;
mod uid;

use dom::component::{Component, ComponentBuilder, ComposedComponent, Constructor, Subscription};
use dom::html::Html;

/// Starts application with component

pub fn run<C: 'static, P: 'static, M: 'static, S: 'static>(id: &str, props: P, children: Vec<Html>)
where
    C: Component<Props = P, Msg = M, Sub = S> + Constructor<Props = P>,
{
    let mut builder = ComponentBuilder::new();
    let component = C::constructor(props, &mut builder);
    let component = ComposedComponent::new(component, builder, Subscription::none());
    component.borrow_mut().set_children(children);
    state::init(component, id);
}

pub mod prelude {
    pub use crate::dom::component::Cmd;
    pub use crate::dom::component::Component;
    pub use crate::dom::component::ComponentBuilder;
    pub use crate::dom::component::Constructor;
    pub use crate::dom::component::Subscription;
    pub use crate::dom::html::Attributes;
    pub use crate::dom::html::Events;
    pub use crate::dom::html::Html;
}