-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter.rs
44 lines (40 loc) · 1.38 KB
/
counter.rs
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
use futures_util::StreamExt;
use wsdom::{js_types::JsValue, Browser};
pub async fn app(browser: Browser) {
// create the UI
let document = wsdom::dom::document(&browser);
let body = document.get_body();
let btn_add = document.create_element(&"button", &wsdom::null());
btn_add.set_inner_text(&"+");
let btn_sub = document.create_element(&"button", &wsdom::null());
btn_sub.set_inner_text(&"-");
let label = document.create_element(&"span", &wsdom::null());
body.append_child(&btn_sub);
body.append_child(&label);
body.append_child(&btn_add);
// start with count = 0
let mut value = 0;
// set up the click listeners
let mut click_add_stream = {
let (callback, func) = wsdom::callback::new_callback::<JsValue>(&browser);
btn_add.add_event_listener(&"click", &func, &wsdom::null());
callback
};
let mut click_sub_stream = {
let (callback, func) = wsdom::callback::new_callback::<JsValue>(&browser);
btn_sub.add_event_listener(&"click", &func, &wsdom::null());
callback
};
// wait for event and update count
loop {
label.set_inner_text(&&*format!("{value}"));
tokio::select! {
_ = click_add_stream.next() => {
value += 1;
}
_ = click_sub_stream.next() => {
value -= 1;
}
}
}
}