Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Other variant to the ListenerKind #2417

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions packages/yew/src/virtual_dom/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,28 @@ macro_rules! gen_listener_kinds {
/// Supported kinds of DOM event listeners
// Using instead of strings to optimise registry collection performance by simplifying
// hashmap hash calculation.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[allow(non_camel_case_types)]
#[allow(missing_docs)]
pub enum ListenerKind {
$( $kind, )*
other(std::borrow::Cow<'static, str>),
Copy link
Member

@siku2 siku2 Jan 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should use proper style for this variant and use upper case Other. This also conveys that it's different from the other variants.

But given that we're dealing with a special enum, this isn't a blocking issue.

}

impl ListenerKind {
pub fn type_name(&self) -> &str {
match self {
Self::other(type_name) => type_name.as_ref(),
kind => &kind.as_ref()[2..],
}
}
}

impl AsRef<str> for ListenerKind {
fn as_ref(&self) -> &str {
match self {
$( Self::$kind => stringify!($kind), )*
Self::other(type_name) => type_name.as_ref(),
}
}
}
Expand Down Expand Up @@ -312,7 +323,7 @@ impl Default for Listeners {
}
}

#[derive(Clone, Copy, Hash, Eq, PartialEq, Debug)]
#[derive(Clone, Hash, Eq, PartialEq, Debug)]
struct EventDescriptor {
kind: ListenerKind,
passive: bool,
Expand Down Expand Up @@ -347,12 +358,13 @@ impl GlobalHandlers {
fn ensure_handled(&mut self, desc: EventDescriptor) {
if !self.handling.contains(&desc) {
let cl = BODY.with(|body| {
let cl = Closure::wrap(
Box::new(move |e: Event| Registry::handle(desc, e)) as Box<dyn Fn(Event)>
);
let cl = Closure::wrap(Box::new({
let desc = desc.clone();
move |e: Event| Registry::handle(desc.clone(), e)
}) as Box<dyn Fn(Event)>);
AsRef::<web_sys::EventTarget>::as_ref(body)
.add_event_listener_with_callback_and_add_event_listener_options(
&desc.kind.as_ref()[2..],
desc.kind.type_name(),
cl.as_ref().unchecked_ref(),
&{
let mut opts = web_sys::AddEventListenerOptions::new();
Expand All @@ -371,7 +383,7 @@ impl GlobalHandlers {
#[cfg(not(test))]
cl.forget();
#[cfg(test)]
self.registered.push((desc.kind, cl));
self.registered.push((desc.kind.clone(), cl));

self.handling.insert(desc);
}
Expand All @@ -386,7 +398,7 @@ impl Drop for GlobalHandlers {
for (kind, cl) in std::mem::take(&mut self.registered) {
AsRef::<web_sys::EventTarget>::as_ref(body)
.remove_event_listener_with_callback(
&kind.as_ref()[2..],
kind.type_name(),
cl.as_ref().unchecked_ref(),
)
.unwrap();
Expand Down Expand Up @@ -421,7 +433,7 @@ impl Registry {
HashMap::<EventDescriptor, Vec<Rc<dyn Listener>>>::with_capacity(listeners.len());
for l in listeners.iter().filter_map(|l| l.as_ref()).cloned() {
let desc = EventDescriptor::from(l.deref());
self.global.ensure_handled(desc);
self.global.ensure_handled(desc.clone());
by_desc.entry(desc).or_default().push(l);
}
self.by_id.insert(id, by_desc);
Expand All @@ -437,7 +449,7 @@ impl Registry {

for l in listeners.iter().filter_map(|l| l.as_ref()).cloned() {
let desc = EventDescriptor::from(l.deref());
self.global.ensure_handled(desc);
self.global.ensure_handled(desc.clone());
by_desc.entry(desc).or_default().push(l);
}
}
Expand Down