Commit 6680450
Restructure
Changed files (2)
Diff
@@ -383,15 +383,11 @@ pub fn get_callback(name: &str) -> Option<js_sys::Function> { /// HTML Component. See kaja-web-component. pub trait Component: Default {- fn connected(_id: &str) {}- fn disconnected(_id: &str) {}- fn observed_attributes() -> &'static [&'static str] {- &[]+ fn connected(&self) {}+ fn disconnected(&self) {}+ fn attribute_changed(&self, name: &str, _old_value: &str, _new_value: &str) {+ log!("Attribute changed but no update handler for ", name); }- fn attribute_changed(id: &str, name: &str, _old_value: &str, _new_value: &str) {- log!("Attribute changed but no update handler for ", id, name);- }- fn update(_id: &str) {} } pub fn get_element_by_id(id: &str) -> Option<HtmlElement> {@@ -5,26 +5,11 @@ use kaja_web::prelude::*; use kaja_web_component::component; use wasm_bindgen_test::*; -use std::rc::Rc;-use std::str::FromStr;--thread_local! {- static TEST2_COMPONENTS: RefCell<HashMap<String, Rc<RefCell<Test2ActualContainer>>>> =- RefCell::new(HashMap::new());-}--fn parse_value<T>(s: &str) -> Option<T>-where- T: FromStr,-{- s.parse::<T>().ok()-}- #[wasm_bindgen_test] fn test_new_component() { common::setup_test(); - create_test2();+ Test2RefContainer::create_component("test-get-set-id"); let id = "test-get-set-id"; @@ -36,44 +21,97 @@ fn test_new_component() { let component = Test2Component::get_component(id); // ref } -fn create_test2() {- let handle = Rc::new(RefCell::new(Test2ActualContainer {- id: "test2".to_string(),- attribute_values: Rc::new(RefCell::new(Test2::default())),- }));-- register_component(handle.clone());- Test2ActualContainer::connect(handle);+#[derive(Default)]+struct Test2 {+ test: bool,+ number: i32, } -fn register_component(component: Rc<RefCell<Test2ActualContainer>>) {- let id = component.borrow().id.clone();+impl Component for Test2Component {+ fn connected(&self) {+ log!("Connected:", self.get_id());+ } - TEST2_COMPONENTS.with(|components| {- components.borrow_mut().insert(id, component);- });+ fn disconnected(&self) {+ log!("Disconnected:", self.get_id());+ }++ fn attribute_changed(&self, name: &str, old_value: &str, new_value: &str) {+ log!(+ "Attribute changed:",+ name,+ "old:",+ old_value,+ "new:",+ new_value+ );+ } } -#[derive(Default)]-struct Test2 {- test: bool,+thread_local! {+ static TEST2_COMPONENTS: RefCell<HashMap<String, std::rc::Rc<std::cell::RefCell<Test2RefContainer>>>> =+ RefCell::new(HashMap::new()); } -struct Test2ActualContainer {+impl Test2 {+ fn set_test_attribute(&mut self, name: &str, value: &str) {+ let _ = self.try_set_test_attribute(name, value);+ }++ fn try_set_test_attribute(&mut self, name: &str, value: &str) -> Result<()> {+ if name == "test" {+ self.test = value.parse().map_err(|_| Error)?;+ } else if name == "number" {+ self.number = value.parse().map_err(|_| Error)?;+ } else {+ error!("No field", name, "in Test2 can't set it to", value);+ return Err(Error);+ }++ Ok(())+ }+}++#[derive(Default)]+struct Test2RefContainer { id: String, attribute_values: Rc<RefCell<Test2>>, } -impl Test2ActualContainer {- fn attributes_update(&self, name: &str, old_value: &str, new_value: &str) {}+impl Test2RefContainer {+ fn create_component(id: &str) {+ let handle = Rc::new(RefCell::new(Test2RefContainer {+ id: id.to_string(),+ attribute_values: std::rc::Rc::new(std::cell::RefCell::new(Test2::default())),+ }));++ Self::register_component(handle.clone());+ let component = Test2Component::get_component(&id);++ if component.is_none() {+ error!("Failed to register compoent.");+ return;+ }++ let component_impl = component.unwrap();+ component_impl.connected();+ }++ fn register_component(component: Rc<RefCell<Test2RefContainer>>) {+ let id = component.borrow().id.clone();++ TEST2_COMPONENTS.with(|components| {+ components.borrow_mut().insert(id, component);+ });+ } } +#[derive(Default)] struct Test2Component {- handle: Rc<RefCell<Test2ActualContainer>>,+ handle: Rc<RefCell<Test2RefContainer>>, } impl Test2Component {- fn connect(&self) {} fn get_component(id: &str) -> Option<Test2Component> { let handle = TEST2_COMPONENTS.with(|components| components.borrow().get(id).cloned()); @@ -86,40 +124,20 @@ impl Test2Component { }) } - fn attributes_update(&self, name: &str, old_value: &str, new_value: &str) {- self.handle- .borrow()- .attributes_update(name, old_value, new_value);+ fn internal_observed_attributes() -> &'static [&'static str] {+ &[] } - fn set_test_attribute(&self, name: &str, value: &str) {}-- fn get_test_attribute(&self) -> String {- String::from("test ...")+ fn internal_attributes_update(&self, name: &str, old_value: &str, new_value: &str) {+ let handle = self.handle.borrow();+ let mut attr = handle.attribute_values.borrow_mut();+ attr.set_test_attribute(name, new_value); } fn get_attributes(&self) -> Rc<RefCell<Test2>> { self.handle.borrow().attribute_values.clone() } - fn with_attributes_mut<F>(&self, f: F)- where- F: FnOnce(&mut Test2),- {- let handle = self.handle.borrow();- let mut attributes = handle.attribute_values.borrow_mut();- f(&mut attributes);- }-- fn with_attributes<F>(&self, f: F)- where- F: FnOnce(&Test2),- {- let handle = self.handle.borrow();- let attributes = handle.attribute_values.borrow();- f(&attributes);- }- fn get_id(&self) -> String { self.handle.borrow().id.clone() }