on master help sign in
TodoMVC: use `?` instead of unwrapping in `run` start function
commit1e3cf1fbe39e63fcb6aa9764087098fdd13ff783 authorNick Fitzgerald <fitzgen@gmail.com> dateTue 12 Feb 2019, 11:06 (8 years ago) parent2271ad4
3 files changed+9−6
Cargo.lock+1 examples/todomvc/Cargo.toml+1 examples/todomvc/src/lib.rs+7−6
Cargo.lock+1
@@ -137,6 +137,7 @@ dependencies = [
137137 "console_log 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
138138 "dodrio 0.1.0",
139139 "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)",
140141 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
141142 "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)",
142143 "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"
88cfg-if = "0.1.6"
99dodrio = { path = "../.." }
1010futures = "0.1.25"
11+js-sys = "0.3.11"
1112serde = { features = ["derive"], version = "1.0.87" }
1213serde_json = "1.0.38"
1314wasm-bindgen = "0.2.34"
examples/todomvc/src/lib.rs+7−6
@@ -20,16 +20,15 @@ use wasm_bindgen::prelude::*;
2020/// Since this is marked `#[wasm_bindgen(start)]` it is automatically invoked
2121/// once the wasm module instantiated on the Web page.
2222#[wasm_bindgen(start)]
23−pub fn run() {
23+pub fn run() -> Result<(), JsValue> {
2424 // Set up the logging for debugging if/when things go wrong.
2525 init_logging();
2626
2727 // Grab the TODO app container.
2828 let document = utils::document();
2929 let container = document
30− .query_selector(".todoapp")
31− .unwrap_throw()
32− .unwrap_throw();
30+ .query_selector(".todoapp")?
31+ .ok_or_else(|| js_sys::Error::new("could not find `.todoapp` container"))?;
3332
3433 // Create a new `Todos` render component.
3534 let todos = Todos::<Controller>::new();
@@ -42,14 +41,16 @@ pub fn run() {
4241
4342 // Run the virtual DOM forever and don't unmount it.
4443 vdom.forget();
44+
45+ Ok(())
4546}
4647
4748cfg_if::cfg_if! {
4849 if #[cfg(feature = "logging")] {
4950 fn init_logging() {
50− use log::*;
5151 console_error_panic_hook::set_once();
52− console_log::init_with_level(Level::Trace).expect_throw("should initialize logging OK");
52+ console_log::init_with_level(log::Level::Trace)
53+ .expect_throw("should initialize logging OK");
5354 }
5455 } else {
5556 fn init_logging() {