on master help sign in
TodoMVC: use `?` instead of unwrapping in `run` start function
1e3cf1fNick Fitzgerald
Cargo.lock+1
@@ -137,6 +137,7 @@ dependencies = [
137 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
137 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
138 "dodrio 0.1.0",
138 "dodrio 0.1.0",
139 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
139 "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
140+ "js-sys 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
140 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
141 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
141 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
142 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
142 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
143 "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
examples/todomvc/Cargo.toml+1
@@ -8,6 +8,7 @@ version = "0.1.0"
8cfg-if = "0.1.6"
8cfg-if = "0.1.6"
9dodrio = { path = "../.." }
9dodrio = { path = "../.." }
10futures = "0.1.25"
10futures = "0.1.25"
11+js-sys = "0.3.11"
11serde = { features = ["derive"], version = "1.0.87" }
12serde = { features = ["derive"], version = "1.0.87" }
12serde_json = "1.0.38"
13serde_json = "1.0.38"
13wasm-bindgen = "0.2.34"
14wasm-bindgen = "0.2.34"
examples/todomvc/src/lib.rs+7−6
@@ -20,16 +20,15 @@ use wasm_bindgen::prelude::*;
20/// Since this is marked `#[wasm_bindgen(start)]` it is automatically invoked
20/// Since this is marked `#[wasm_bindgen(start)]` it is automatically invoked
21/// once the wasm module instantiated on the Web page.
21/// once the wasm module instantiated on the Web page.
22#[wasm_bindgen(start)]
22#[wasm_bindgen(start)]
23−pub fn run() {
23+pub fn run() -> Result<(), JsValue> {
24 // Set up the logging for debugging if/when things go wrong.
24 // Set up the logging for debugging if/when things go wrong.
25 init_logging();
25 init_logging();
26
26
27 // Grab the TODO app container.
27 // Grab the TODO app container.
28 let document = utils::document();
28 let document = utils::document();
29 let container = document
29 let container = document
30− .query_selector(".todoapp")
30+ .query_selector(".todoapp")?
31− .unwrap_throw()
31+ .ok_or_else(|| js_sys::Error::new("could not find `.todoapp` container"))?;
32− .unwrap_throw();
33
32
34 // Create a new `Todos` render component.
33 // Create a new `Todos` render component.
35 let todos = Todos::<Controller>::new();
34 let todos = Todos::<Controller>::new();
@@ -42,14 +41,16 @@ pub fn run() {
42
41
43 // Run the virtual DOM forever and don't unmount it.
42 // Run the virtual DOM forever and don't unmount it.
44 vdom.forget();
43 vdom.forget();
44+
45+ Ok(())
45}
46}
46
47
47cfg_if::cfg_if! {
48cfg_if::cfg_if! {
48 if #[cfg(feature = "logging")] {
49 if #[cfg(feature = "logging")] {
49 fn init_logging() {
50 fn init_logging() {
50− use log::*;
51 console_error_panic_hook::set_once();
51 console_error_panic_hook::set_once();
52+ console_log::init_with_level(log::Level::Trace)
52− console_log::init_with_level(Level::Trace).expect_throw("should initialize logging OK");
53+ .expect_throw("should initialize logging OK");
53 }
54 }
54 } else {
55 } else {
55 fn init_logging() {
56 fn init_logging() {