Skip to content

Commit

Permalink
Add pending event listener on the VTag (#2300)
Browse files Browse the repository at this point in the history
* Add pending event listener on the VTag

* Remove deprecated set_listener and rename add_listener

* Add return value comment
  • Loading branch information
XX committed Dec 28, 2021
1 parent 5181e1a commit f0fb406
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::borrow::Cow;
use std::cmp::PartialEq;
use std::hint::unreachable_unchecked;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::rc::Rc;
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -430,8 +431,22 @@ impl VTag {
.insert(key, value.into_prop_value());
}

/// Add event listener on the [VTag]'s [Element].
/// Returns `true` if the listener has been added, `false` otherwise.
pub fn add_listener(&mut self, listener: Rc<dyn Listener>) -> bool {
if let Listeners::Pending(listeners) = &mut self.listeners {
let mut listeners = mem::take(listeners).into_vec();
listeners.push(Some(listener));

self.set_listeners(listeners.into_boxed_slice());
true
} else {
false
}
}

/// Set event listeners on the [VTag]'s [Element]
pub fn set_listener(&mut self, listeners: Box<[Option<Rc<dyn Listener>>]>) {
pub fn set_listeners(&mut self, listeners: Box<[Option<Rc<dyn Listener>>]>) {
self.listeners = Listeners::Pending(listeners);
}

Expand Down

0 comments on commit f0fb406

Please sign in to comment.