Commit 6b30cee
Callback updated
Changed files (4)
modifiedexamples/counter-component/src/counter.rsmodifiedexamples/counter-component/src/lib.rsdeletedexamples/counter-component/src/main.rsmodifiedsrc/lib.rs
Diff
@@ -15,7 +15,6 @@ impl Component for Counter { } fn observed_attributes() -> &'static [&'static str] {- log!("fetching observed attributes"); &["count"] } @@ -26,7 +25,6 @@ impl Component for Counter { old_value: &str, new_value: &str, ) {- log!("change", name, "from", old_value, "to", new_value); self.render(&element); } }@@ -62,8 +60,6 @@ impl Counter { #[callback(increment)] pub fn increment(id: String) {- log!("increment");- let element = get_component_element(id.as_str()); if element.is_none() {@@ -86,7 +82,7 @@ pub fn increment(id: String) { let new_count = count + 1; if let Some(counter_arc) = get_component::<Counter>(id.as_str()) {- let mut counter = counter_arc.write().unwrap();+ let mut counter = counter_arc.lock().unwrap(); counter.value = new_count; counter.render(&html_element); } else {@@ -1,10 +1,10 @@ use kaja_web::prelude::*; mod counter;-mod main;+mod main_component; use counter::*;-use main::*;+use main_component::*; #[wasm_bindgen(start)] pub fn init() {deleted file mode 100644@@ -1,23 +0,0 @@-use kaja_web::prelude::*;--#[component("main-component")]-pub struct Main {- message: Option<String>,-}--impl Component for Main {- fn connected(&mut self, element: HtmlElement) {- self.render(element);- }-}--impl Main {- fn render(&self, element: HtmlElement) {- let content = html! {{- <div><counter-component count="1" /></div>- <div><counter-component count="42" /></div>- }};-- element.set_inner_html(content.as_str());- }-}@@ -25,6 +25,14 @@ pub static NEXT_COMPONENT_COUNTER: std::sync::atomic::AtomicU32 = #[derive(Debug, Clone, Copy)] pub struct Error; +impl std::fmt::Display for Error {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(f, "kaja_web::Error")+ }+}++impl std::error::Error for Error {}+ pub type Result<T> = std::result::Result<T, Error>; pub struct CallbackRegistration {@@ -60,17 +68,11 @@ inventory::collect!(CallbackRegistration); /// }}; /// ``` pub fn init_callbacks() {- log!("init_callbacks");-- log!("init_callbacks functions"); for init in inventory::iter::<InitFn> {- log!("init fn"); (init.0)(); } - log!("init_callbacks components"); for init in inventory::iter::<InitComponentFn> {- log!("init component"); (init.0)(); } }@@ -346,14 +348,14 @@ pub fn get_component_element(id: &str) -> Option<HtmlElement> { } pub trait ComponentStorage: Sized {- fn storage() -> &'static RwLock<Option<HashMap<String, std::sync::Arc<std::sync::RwLock<Self>>>>>;+ fn storage() -> &'static RwLock<Option<HashMap<String, std::sync::Arc<std::sync::Mutex<Self>>>>>; } /// Returns a cloned Arc pointing to the stored component. The component itself is /// protected by an `RwLock` so callers can lock it for mutable or shared access when needed. pub fn get_component<T: ComponentStorage + 'static>( id: &str,-) -> Option<std::sync::Arc<std::sync::RwLock<T>>> {+) -> Option<std::sync::Arc<std::sync::Mutex<T>>> { let map_lock = T::storage().read(); if map_lock.is_err() {