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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![allow(unused_macros)]
pub use crate::{
fetch::{spawn_local, Method, Request},
routing::{push_route, push_path, Url},
util::{document, window},
vdom::App,
websys_bridge::{to_html_el, to_input, to_kbevent, to_mouse_event, to_select, to_textarea},
};
use wasm_bindgen::{closure::Closure, JsCast};
pub mod dom_types;
pub mod fetch;
pub mod routing;
pub mod shortcuts;
pub mod storage;
mod util;
mod vdom;
mod websys_bridge;
pub fn empty<Ms>() -> dom_types::El<Ms> {
let mut el = dom_types::El::empty(dom_types::Tag::Del);
el.add_attr("dummy-element".into(), "true".into());
el
}
pub fn log<S: ToString>(text: S) {
web_sys::console::log_1(&text.to_string().into());
}
pub fn set_interval(handler: Box<Fn()>, timeout: i32) {
let callback = Closure::wrap(handler as Box<dyn Fn()>);
let window = web_sys::window().expect("Can't find window");
window
.set_interval_with_callback_and_timeout_and_arguments_0(
callback.as_ref().unchecked_ref(),
timeout,
)
.expect("Problem setting interval");
callback.forget();
}
pub fn set_timeout(handler: Box<Fn()>, timeout: i32) {
let callback = Closure::wrap(handler as Box<dyn Fn()>);
let window = web_sys::window().expect("Can't find window");
window
.set_timeout_with_callback_and_timeout_and_arguments_0(
callback.as_ref().unchecked_ref(),
timeout,
)
.expect("Problem setting timeout");
callback.forget();
}
pub mod prelude {
pub use crate::{
dom_types::{
did_mount, did_update, input_ev, keyboard_ev, mouse_ev, raw_ev, simple_ev,
will_unmount, At, El, Ev, Tag, UpdateEl,
},
shortcuts::*,
vdom::{Update, Update::Render, Update::Skip},
};
pub use std::collections::HashMap;
pub use wasm_bindgen::prelude::*;
}
#[cfg(test)]
pub mod tests {
use wasm_bindgen_test::wasm_bindgen_test_configure;
wasm_bindgen_test_configure!(run_in_browser);
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
pub fn app_builds() {
use crate as seed;
use crate::prelude::*;
use crate::{
div,
dom_types::{El, At, UpdateEl, mouse_ev},
vdom::Update,
};
#[derive(Clone)]
struct Model {
pub val: i32,
}
impl Default for Model {
fn default() -> Self {
Self { val: 0 }
}
}
#[derive(Clone)]
enum Msg {
Increment,
}
fn update(msg: Msg, model: Model) -> Update<Model> {
match msg {
Msg::Increment => Update::Render(Model { val: model.val + 1 }),
}
}
fn view(_state: seed::App<Msg, Model>, model: &Model) -> El<Msg> {
div!["Hello world"]
}
fn window_events(model: &Model) -> Vec<seed::dom_types::Listener<Msg>> {
vec![mouse_ev("mousemove", |_| Msg::Increment)]
}
fn routes(url: &seed::Url) -> Msg {
Msg::Increment
}
#[wasm_bindgen]
pub fn render() {
seed::App::build(Model::default(), update, view)
.mount("body")
.routes(routes)
.window_events(window_events)
.finish()
.run();
}
}
}