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
use dodrio::VdomWeak;
use rust_wasm_websys_utils::websysmod;
use unwrap::unwrap;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::spawn_local;
pub trait RouterTrait {
fn get_location_hash(&self) -> &str;
fn get_file_name_to_fetch(&self) -> &str;
fn set_file_name_to_fetch(&mut self, file_name_to_fetch: String, vdom: VdomWeak) -> String;
fn from_root_to_router_data(root: &mut dyn dodrio::RootRender) -> &mut Self;
fn fn_to_prepare_fetched_html_for_render(
resp_body_text: String,
) -> Box<dyn Fn(&mut dyn dodrio::RootRender) + 'static>;
fn start_router(&self, vdom: VdomWeak) {
let on_hash_change = Box::new(move || {
let location = websysmod::window().location();
let location_hash_new = unwrap!(location.hash());
wasm_bindgen_futures::spawn_local({
let vdom_on_next_tick = vdom.clone();
async move {
let _ = vdom_on_next_tick
.with_component({
let vdom = vdom_on_next_tick.clone();
move |root| {
let router_data = Self::from_root_to_router_data(root);
let location_hash_new = location_hash_new.clone();
if router_data.get_location_hash() != location_hash_new {
let url = router_data
.set_file_name_to_fetch(location_hash_new, vdom.clone());
spawn_local({
let vdom_on_next_tick = vdom.clone();
async move {
let resp_body_text: String =
websysmod::fetch_response(url).await;
unwrap!(
vdom_on_next_tick
.with_component({
Self::fn_to_prepare_fetched_html_for_render(
resp_body_text,
)
})
.await
);
vdom.schedule_render();
}
});
}
}
})
.await;
}
});
});
self.set_on_hash_change_callback(on_hash_change);
}
fn set_on_hash_change_callback(&self, mut on_hash_change: Box<dyn FnMut()>) {
on_hash_change();
let on_hash_change = Closure::wrap(on_hash_change);
websysmod::window()
.add_event_listener_with_callback("hashchange", on_hash_change.as_ref().unchecked_ref())
.unwrap_throw();
on_hash_change.forget();
}
}
pub fn get_url_param_in_hash_after_dot(location_hash: &str) -> &str {
let mut spl = location_hash.split('.');
unwrap!(spl.next());
unwrap!(spl.next())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_url_param_in_hash_after_dot() {
let x = get_url_param_in_hash_after_dot("#p03.1234");
assert_eq!("1234", x);
}
}