Allow mutable forms of the API

This commit is contained in:
Jeremy Wall 2023-11-21 23:03:41 -05:00
parent 7b589373ed
commit a63670b7d3
2 changed files with 53 additions and 7 deletions

View File

@ -104,6 +104,7 @@ fn expand_component_def(
} }
} }
// TODO(jwall): Stateful elements?
fn expand_wc_struct_trait_shim( fn expand_wc_struct_trait_shim(
struct_name: &Ident, struct_name: &Ident,
once_name: &Ident, once_name: &Ident,
@ -224,45 +225,51 @@ fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl {
} }
#[::wasm_bindgen::prelude::wasm_bindgen] #[::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; use #trait_path;
self.init(element); self.init(element);
self.init_mut(element);
} }
#[::wasm_bindgen::prelude::wasm_bindgen] #[::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; use #trait_path;
self.connected(element); self.connected(element);
self.connected_mut(element);
} }
#[::wasm_bindgen::prelude::wasm_bindgen] #[::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; use #trait_path;
self.disconnected(element); self.disconnected(element);
self.disconnected_mut(element);
} }
#[::wasm_bindgen::prelude::wasm_bindgen] #[::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; use #trait_path;
self.adopted(element); self.adopted(element);
self.adopted_mut(element);
} }
#[::wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn attribute_changed_impl( pub fn attribute_changed_impl(
&self, &mut self,
element: &web_sys::HtmlElement, element: &web_sys::HtmlElement,
name: ::wasm_bindgen::JsValue, name: ::wasm_bindgen::JsValue,
old_value: ::wasm_bindgen::JsValue, old_value: ::wasm_bindgen::JsValue,
new_value: ::wasm_bindgen::JsValue, new_value: ::wasm_bindgen::JsValue,
) { ) {
use #trait_path; 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; use #trait_path;
self.handle_event(element, event); self.handle_event(element, event);
self.handle_event_mut(element, event);
} }
} }
} }

View File

@ -159,6 +159,10 @@ pub trait WebComponentBinding: WebComponentDef {
// noop // noop
} }
fn init_mut(&mut self, _element: &HtmlElement) {
// noop
}
/// Called when the web component is connected to the DOM. /// Called when the web component is connected to the DOM.
/// This is when you should do any setup like attaching a ShadowDom /// This is when you should do any setup like attaching a ShadowDom
/// or appending elements. /// or appending elements.
@ -166,16 +170,33 @@ pub trait WebComponentBinding: WebComponentDef {
// noop // 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. /// Called when the web component is disconnected from the DOM.
fn disconnected(&self, _element: &HtmlElement) { fn disconnected(&self, _element: &HtmlElement) {
// noop // 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. /// Called When the web component is moved to a new document.
fn adopted(&self, _element: &HtmlElement) { fn adopted(&self, _element: &HtmlElement) {
// noop // 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. /// Called when one of the observed attributes has changed.
/// the observedc attributes are listed in the observed_attrs argument to the /// the observedc attributes are listed in the observed_attrs argument to the
/// `#[web_component(observed_attrs = "['attr1', 'attr2']")` attribute. /// `#[web_component(observed_attrs = "['attr1', 'attr2']")` attribute.
@ -189,10 +210,28 @@ pub trait WebComponentBinding: WebComponentDef {
// noop // 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. /// Top level event handler for this custom element.
fn handle_event(&self, _element: &HtmlElement, _event: &Event) { fn handle_event(&self, _element: &HtmlElement, _event: &Event) {
// noop // 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 /// Marker trait used in the generated shims to assert that there are Rust implemtntations