forked from fitzgen/dodrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
executable file
·87 lines (73 loc) · 2.72 KB
/
lib.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
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
use dodrio::{bumpalo, Node, Render, RenderContext};
use log::*;
use wasm_bindgen::prelude::*;
/// A counter that can be incremented and decremented!
struct Counter {
count: isize,
}
impl Counter {
/// Construct a new, zeroed counter.
fn new() -> Counter {
Counter { count: 0 }
}
/// Increment this counter's count.
fn increment(&mut self) {
self.count += 1;
}
/// Decrement this counter's count.
fn decrement(&mut self) {
self.count -= 1;
}
}
// The `Render` implementation for `Counter`s displays the current count and has
// buttons to increment and decrement the count.
impl<'a> Render<'a> for Counter {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
use dodrio::builder::*;
// Stringify the count as a bump-allocated string.
let count = bumpalo::format!(in cx.bump, "{}", self.count);
div(&cx)
.children([
button(&cx)
.on("click", |root, vdom, _event| {
// Cast the root render component to a `Counter`, since
// we know that's what it is.
let counter = root.unwrap_mut::<Counter>();
// Increment the counter.
counter.increment();
// Since the count has updated, we should re-render the
// counter on the next animation frame.
vdom.schedule_render();
})
.children([text("+")])
.finish(),
text(count.into_bump_str()),
button(&cx)
.on("click", |root, vdom, _event| {
// Same as above, but decrementing instead of incrementing.
let counter = root.unwrap_mut::<Counter>();
counter.decrement();
vdom.schedule_render();
})
.children([text("-")])
.finish(),
])
.finish()
}
}
#[wasm_bindgen(start)]
pub fn run() {
// Initialize debug logging for if/when things go wrong.
console_error_panic_hook::set_once();
console_log::init_with_level(Level::Trace).expect("should initialize logging OK");
// Get the document's `<body>`.
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
// Construct a new counter component.
let counter = Counter::new();
// Mount our counter component to the `<body>`.
let vdom = dodrio::Vdom::new(&body, counter);
// Run the virtual DOM and its listeners forever.
vdom.forget();
}