Fix some ambiguous wasm_bindgen crate references.

This commit is contained in:
Jeremy Wall 2023-11-21 19:44:14 -05:00
parent 8ce79d34a5
commit 7b589373ed
2 changed files with 24 additions and 21 deletions

View File

@ -130,9 +130,9 @@ fn expand_wc_struct_trait_shim(
} }
#[doc = "Defines this web component element if not defined already otherwise returns an error."] #[doc = "Defines this web component element if not defined already otherwise returns an error."]
pub fn define() -> std::result::Result<#handle_path, wasm_bindgen::JsValue> { pub fn define() -> std::result::Result<#handle_path, ::wasm_bindgen::JsValue> {
use wasm_bindgen::JsCast; use ::wasm_bindgen::JsCast;
use web_sys::{window, Element, HtmlElement}; use web_sys::{Element, HtmlElement};
let registry = web_sys::window().unwrap().custom_elements(); let registry = web_sys::window().unwrap().custom_elements();
let maybe_element = registry.get(Self::element_name()); let maybe_element = registry.get(Self::element_name());
if maybe_element.is_truthy() { if maybe_element.is_truthy() {
@ -198,10 +198,10 @@ return element;",
let obj = Self::new(); let obj = Self::new();
obj obj
}); });
let constructor_handle = wasm_bindgen::prelude::Closure::wrap(f).into_js_value().unchecked_into::<js_sys::Function>(); let constructor_handle = ::wasm_bindgen::prelude::Closure::wrap(f).into_js_value().unchecked_into::<js_sys::Function>();
let element = fun let element = fun
.call1( .call1(
&window().unwrap(), &web_sys::window().unwrap(),
constructor_handle.as_ref(), constructor_handle.as_ref(),
)? )?
.dyn_into()?; .dyn_into()?;
@ -216,45 +216,45 @@ return element;",
fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl { fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl {
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentBinding)); let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentBinding));
parse_quote! { parse_quote! {
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
impl #struct_name { impl #struct_name {
#[wasm_bindgen::prelude::wasm_bindgen(constructor)] #[::wasm_bindgen::prelude::wasm_bindgen(constructor)]
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn init_impl(&self, element: &web_sys::HtmlElement) { pub fn init_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path; use #trait_path;
self.init(element); self.init(element);
} }
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn connected_impl(&self, element: &web_sys::HtmlElement) { pub fn connected_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path; use #trait_path;
self.connected(element); self.connected(element);
} }
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn disconnected_impl(&self, element: &web_sys::HtmlElement) { pub fn disconnected_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path; use #trait_path;
self.disconnected(element); self.disconnected(element);
} }
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn adopted_impl(&self, element: &web_sys::HtmlElement) { pub fn adopted_impl(&self, element: &web_sys::HtmlElement) {
use #trait_path; use #trait_path;
self.adopted(element); self.adopted(element);
} }
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
pub fn attribute_changed_impl( pub fn attribute_changed_impl(
&self, &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, old_value, new_value);
@ -293,11 +293,9 @@ fn expand_web_component_struct(
let wasm_shim = expand_wasm_shim(&struct_name); let wasm_shim = expand_wasm_shim(&struct_name);
let binding_trait = expand_binding(&struct_name); let binding_trait = expand_binding(&struct_name);
let expanded = quote! { let expanded = quote! {
use std::sync::Once;
use wasm_bindgen;
#[allow(non_snake_case)] #[allow(non_snake_case)]
static #struct_once_name: Once = Once::new(); static #struct_once_name: std::sync::Once = std::sync::Once::new();
#[wasm_bindgen::prelude::wasm_bindgen] #[::wasm_bindgen::prelude::wasm_bindgen]
#[derive(Default, Debug)] #[derive(Default, Debug)]
#item_struct #item_struct
#component_def #component_def
@ -318,9 +316,8 @@ fn expand_template_struct(item_struct: ItemStruct) -> TokenStream {
); );
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(TemplateElement)); let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(TemplateElement));
let expanded = quote! { let expanded = quote! {
use std::sync::OnceLock;
use web_sys::Node; use web_sys::Node;
static #struct_once_name: OnceLock<Option<String>> = OnceLock::new(); static #struct_once_name: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
#item_struct #item_struct
impl #trait_path for #struct_name {} impl #trait_path for #struct_name {}
impl #struct_name { impl #struct_name {

View File

@ -1,4 +1,5 @@
use js_sys::Function; use js_sys::Function;
use wasm_bindgen::JsCast;
use wasm_bindgen::{convert::IntoWasmAbi, JsValue}; use wasm_bindgen::{convert::IntoWasmAbi, JsValue};
#[cfg(feature = "HtmlTemplateElement")] #[cfg(feature = "HtmlTemplateElement")]
use web_sys::HtmlTemplateElement; use web_sys::HtmlTemplateElement;
@ -140,6 +141,11 @@ pub trait WebComponentDef: IntoWasmAbi + Default {
.expect("Failed to create element") .expect("Failed to create element")
} }
/// Creates a custom event
fn custom_event(event_type: &str) -> web_sys::Event {
web_sys::CustomEvent::new(event_type).unwrap().dyn_into().unwrap()
}
fn element_name() -> &'static str; fn element_name() -> &'static str;
fn class_name() -> &'static str; fn class_name() -> &'static str;
} }