diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 92d668f..aa72959 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -104,6 +104,7 @@ fn expand_component_def( } } +// TODO(jwall): Stateful elements? fn expand_wc_struct_trait_shim( struct_name: &Ident, once_name: &Ident, @@ -224,45 +225,51 @@ fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl { } #[::wasm_bindgen::prelude::wasm_bindgen] - pub fn init_impl(&self, element: &web_sys::HtmlElement) { + pub fn init_impl(&mut self, element: &web_sys::HtmlElement) { use #trait_path; self.init(element); + self.init_mut(element); } #[::wasm_bindgen::prelude::wasm_bindgen] - pub fn connected_impl(&self, element: &web_sys::HtmlElement) { + pub fn connected_impl(&mut self, element: &web_sys::HtmlElement) { use #trait_path; self.connected(element); + self.connected_mut(element); } #[::wasm_bindgen::prelude::wasm_bindgen] - pub fn disconnected_impl(&self, element: &web_sys::HtmlElement) { + pub fn disconnected_impl(&mut self, element: &web_sys::HtmlElement) { use #trait_path; self.disconnected(element); + self.disconnected_mut(element); } #[::wasm_bindgen::prelude::wasm_bindgen] - pub fn adopted_impl(&self, element: &web_sys::HtmlElement) { + pub fn adopted_impl(&mut self, element: &web_sys::HtmlElement) { use #trait_path; self.adopted(element); + self.adopted_mut(element); } #[::wasm_bindgen::prelude::wasm_bindgen] pub fn attribute_changed_impl( - &self, + &mut self, element: &web_sys::HtmlElement, name: ::wasm_bindgen::JsValue, old_value: ::wasm_bindgen::JsValue, new_value: ::wasm_bindgen::JsValue, ) { use #trait_path; - self.attribute_changed(element, name, old_value, new_value); + self.attribute_changed(element, name.clone(), old_value.clone(), new_value.clone()); + self.attribute_changed_mut(element, name, old_value, new_value); } - pub fn handle_component_event_impl(&self, element: &web_sys::HtmlElement, event: &web_sys::Event) { + pub fn handle_component_event_impl(&mut self, element: &web_sys::HtmlElement, event: &web_sys::Event) { use #trait_path; self.handle_event(element, event); + self.handle_event_mut(element, event); } } } diff --git a/wasm-web-component/src/lib.rs b/wasm-web-component/src/lib.rs index 483cc56..4fe87c8 100644 --- a/wasm-web-component/src/lib.rs +++ b/wasm-web-component/src/lib.rs @@ -159,6 +159,10 @@ pub trait WebComponentBinding: WebComponentDef { // noop } + fn init_mut(&mut self, _element: &HtmlElement) { + // noop + } + /// Called when the web component is connected to the DOM. /// This is when you should do any setup like attaching a ShadowDom /// or appending elements. @@ -166,16 +170,33 @@ pub trait WebComponentBinding: WebComponentDef { // noop } + /// Called when the web component is connected to the DOM. + /// This is when you should do any setup like attaching a ShadowDom + /// or appending elements. + fn connected_mut(&mut self, _element: &HtmlElement) { + // noop + } + /// Called when the web component is disconnected from the DOM. fn disconnected(&self, _element: &HtmlElement) { // noop } + /// Called when the web component is disconnected from the DOM. + fn disconnected_mut(&mut self, _element: &HtmlElement) { + // noop + } + /// Called When the web component is moved to a new document. fn adopted(&self, _element: &HtmlElement) { // noop } + /// Called When the web component is moved to a new document. + fn adopted_mut(&mut self, _element: &HtmlElement) { + // noop + } + /// Called when one of the observed attributes has changed. /// the observedc attributes are listed in the observed_attrs argument to the /// `#[web_component(observed_attrs = "['attr1', 'attr2']")` attribute. @@ -189,10 +210,28 @@ pub trait WebComponentBinding: WebComponentDef { // noop } + /// Called when one of the observed attributes has changed. + /// the observedc attributes are listed in the observed_attrs argument to the + /// `#[web_component(observed_attrs = "['attr1', 'attr2']")` attribute. + fn attribute_changed_mut( + &mut self, + _element: &HtmlElement, + _name: JsValue, + _old_value: JsValue, + _new_value: JsValue, + ) { + // noop + } + /// Top level event handler for this custom element. fn handle_event(&self, _element: &HtmlElement, _event: &Event) { // noop } + + /// Top level event handler for this custom element. + fn handle_event_mut(&mut self, _element: &HtmlElement, _event: &Event) { + // noop + } } /// Marker trait used in the generated shims to assert that there are Rust implemtntations