bug: Fix some bugs in the init wiring

This commit is contained in:
Jeremy Wall 2023-11-13 18:28:11 -05:00
parent 76baa1871e
commit f41a9d3100
2 changed files with 14 additions and 10 deletions

View File

@ -142,11 +142,10 @@ fn expand_wc_struct_trait_shim(
"class {name} extends HTMLElement {{
constructor() {{
super();
var self = this;
self._impl = impl();
self._impl.init();
for (const t of self.getObservedEvents()) {{
self.addEventListener(t, function(evt) {{ self.handleComponentEvent(evt); }} );
this._impl = impl();
this._impl.init_impl(this);
for (const t of this.observedEvents()) {{
this.addEventListener(t, function(evt) {{ this.handleComponentEvent(evt); }} );
}}
}}
@ -217,6 +216,12 @@ fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl {
Self::default()
}
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn init_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path;
self.init(element);
}
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn connected_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path;

View File

@ -123,15 +123,15 @@ pub trait WebComponentDef: IntoWasmAbi + Default {
}
fn create() -> Element {
Self::create_in_window(window().unwrap())
Self::create_in_window(window().expect("Failed to get window"))
}
fn create_in_window(window: Window) -> Element {
window
.document()
.unwrap()
.expect("Failed to get document")
.create_element(Self::element_name())
.unwrap()
.expect("Failed to create element")
}
fn element_name() -> &'static str;
@ -304,7 +304,7 @@ mod tests {
#[web_component(
class_name = "MyElement",
element_name = "my-element",
observed_attrs = "['class']"
observed_attrs = "['class']",
)]
pub struct MyElementImpl {}
@ -383,7 +383,6 @@ mod tests {
"Added a text node on adopt"
);
}
// Then we can have the new document adopt this node.
}
#[wasm_bindgen_test]