From bf8c7bd1c8584bc16d770340a3a53feeb5ba1026 Mon Sep 17 00:00:00 2001 From: tiye Date: Tue, 30 Jul 2024 15:53:28 +0800 Subject: [PATCH] refine error logs; tag 0.1.11 --- Cargo.lock | 2 +- respo/Cargo.toml | 2 +- respo/src/app.rs | 6 ++++-- respo/src/app/patch.rs | 6 +++--- respo/src/app/renderer.rs | 14 ++++++-------- respo/src/app/util.rs | 21 +++++++++++++++++++-- respo/src/ui/dialog.rs | 4 ++-- respo/src/ui/dialog/confirm.rs | 2 +- respo/src/ui/dialog/prompt.rs | 2 +- 9 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f6237b..f4c7c99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,7 +195,7 @@ dependencies = [ [[package]] name = "respo" -version = "0.1.10" +version = "0.1.11" dependencies = [ "cirru_parser", "js-sys", diff --git a/respo/Cargo.toml b/respo/Cargo.toml index 87ea7ce..4128ea6 100644 --- a/respo/Cargo.toml +++ b/respo/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "respo" -version = "0.1.10" +version = "0.1.11" edition = "2021" description = "a tiny virtual DOM library migrated from ClojureScript" license = "Apache-2.0" diff --git a/respo/src/app.rs b/respo/src/app.rs index 6f8d56a..65c75e5 100644 --- a/respo/src/app.rs +++ b/respo/src/app.rs @@ -82,7 +82,9 @@ pub trait RespoApp { DispatchFn::new(dispatch_action), Self::get_loop_delay(), ) - .expect("rendering node"); + .unwrap_or_else(|e| { + util::error_log!("render loop error: {:?}", e); + }); Ok(()) } @@ -123,7 +125,7 @@ pub trait RespoApp { *store.borrow_mut() = s; } Err(e) => { - util::log!("error: {:?}", e); + util::error_log!("error: {:?}", e); } }, _ => { diff --git a/respo/src/app/patch.rs b/respo/src/app/patch.rs index e108c99..b5878dc 100644 --- a/respo/src/app/patch.rs +++ b/respo/src/app/patch.rs @@ -59,7 +59,7 @@ where } } } else { - crate::util::log!("expected component for effects, got: {}", target_tree); + crate::util::warn_log!("expected component for effects, got: {}", target_tree); } } } @@ -243,7 +243,7 @@ where } } } else { - crate::util::log!("expected component for effects, got: {}", target_tree); + crate::util::warn_log!("expected component for effects, got: {}", target_tree); } } } @@ -272,7 +272,7 @@ where } } } else { - crate::util::log!("expected component for effects, got: {}", target_tree); + crate::util::warn_log!("expected component for effects, got: {}", target_tree); } } } diff --git a/respo/src/app/renderer.rs b/respo/src/app/renderer.rs index 494d5fa..623a3a8 100644 --- a/respo/src/app/renderer.rs +++ b/respo/src/app/renderer.rs @@ -25,8 +25,8 @@ fn drain_rerender_status() -> bool { let ret = { *NEED_TO_ERENDER.read().expect("to drain rerender status") }; if ret { - let mut need_to_erender = NEED_TO_ERENDER.write().expect("to drain rerender status"); - *need_to_erender = false; + let mut need_to_rerender = NEED_TO_ERENDER.write().expect("to drain rerender status"); + *need_to_rerender = false; } ret } @@ -108,17 +108,15 @@ where let mut changes: Vec> = vec![]; diff_tree(&new_tree, &to_prev_tree.borrow(), &Vec::new(), &Vec::new(), &mut changes)?; + // use cirru_parser::CirruWriterOptions; // util::log!( // "prev tree: {}", - // cirru_parser::format( - // &[to_prev_tree2.borrow().to_owned().into()], - // cirru_parser::CirruWriterOptions { use_inline: true } - // ) - // .unwrap() + // cirru_parser::format(&[to_prev_tree.borrow().to_owned().into()], CirruWriterOptions { use_inline: true }).unwrap() // ); + // use crate::dom_change::changes_to_cirru; // util::log!( // "changes: {}", - // cirru_parser::format(&[changes_to_cirru(&changes)], cirru_parser::CirruWriterOptions { use_inline: true }).unwrap() + // cirru_parser::format(&[changes_to_cirru(&changes)], CirruWriterOptions { use_inline: true }).unwrap() // ); let handler = handle_event.to_owned(); diff --git a/respo/src/app/util.rs b/respo/src/app/util.rs index ab12183..1a1c203 100644 --- a/respo/src/app/util.rs +++ b/respo/src/app/util.rs @@ -40,7 +40,10 @@ pub fn raf_loop_slow(interval: i32, mut cb: Box Result<(), String *g.borrow_mut() = Some(Closure::wrap(Box::new(move || { if let Err(e) = cb() { - crate::log!("failed in slow loop: {}", e); + crate::warn_log!( + "Failure in slow loop, program has to stop since inconsistent states. Details: {}", + e + ); } let h = Closure::wrap(Box::new({ @@ -95,7 +98,7 @@ macro_rules! log { /// /// use it like: /// ```ignore -/// util::warn!("a is {}", a); +/// util::warn_log!("a is {}", a); /// ``` #[macro_export] macro_rules! warn_log { @@ -104,6 +107,20 @@ macro_rules! warn_log { }}; } +/// wraps on top of `web_sys::console.error_1`. +/// +/// use it like: +/// ```ignore +/// util::error_log!("a is {}", a); +/// ``` +#[macro_export] +macro_rules! error_log { + ($($t:tt)*) => {{ + web_sys::console::error_1(&format!($($t)*).into()); + }}; +} + +pub use error_log; pub use log; pub use warn_log; diff --git a/respo/src/ui/dialog.rs b/respo/src/ui/dialog.rs index 9d7da40..80fa865 100644 --- a/respo/src/ui/dialog.rs +++ b/respo/src/ui/dialog.rs @@ -105,7 +105,7 @@ impl RespoEffect for EffectModalFade { delay_call.forget(); } None => { - util::log!("content not found"); + util::warn_log!("content not found"); } } } @@ -181,7 +181,7 @@ impl RespoEffect for EffectDrawerFade { delay_call.forget(); } None => { - app::util::log!("content not found"); + app::util::warn_log!("content not found"); } } } diff --git a/respo/src/ui/dialog/confirm.rs b/respo/src/ui/dialog/confirm.rs index 4596d2f..60c4591 100644 --- a/respo/src/ui/dialog/confirm.rs +++ b/respo/src/ui/dialog/confirm.rs @@ -230,7 +230,7 @@ where let window = web_sys::window().unwrap(); // dirty global variable to store a shared callback if let Err(e) = Reflect::set(&window, &JsValue::from_str(NEXT_TASK_NAME), task.as_ref()) { - app::util::log!("failed to store next task {:?}", e); + app::util::error_log!("failed to store next task {:?}", e); } task.forget(); dispatch.run_state(&self.cursor, s)?; diff --git a/respo/src/ui/dialog/prompt.rs b/respo/src/ui/dialog/prompt.rs index 2cf0e8c..8155cac 100644 --- a/respo/src/ui/dialog/prompt.rs +++ b/respo/src/ui/dialog/prompt.rs @@ -348,7 +348,7 @@ where let window = web_sys::window().unwrap(); // dirty global variable to store a shared callback if let Err(e) = Reflect::set(&window, &JsValue::from_str(NEXT_TASK_NAME), task.as_ref()) { - util::log!("failed to store next task {:?}", e); + util::error_log!("failed to store next task {:?}", e); } task.forget(); dispatch.run_state(&self.cursor, s)?;