Skip to content

gloo_event: Take events by reference #82

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

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
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
26 changes: 13 additions & 13 deletions crates/events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ thread_local! {
pub struct EventListener {
target: EventTarget,
event_type: Cow<'static, str>,
callback: Option<Closure<FnMut(Event)>>,
callback: Option<Closure<FnMut(&Event)>>,
phase: EventListenerPhase,
}

Expand All @@ -239,7 +239,7 @@ impl EventListener {
fn raw_new(
target: &EventTarget,
event_type: Cow<'static, str>,
callback: Closure<FnMut(Event)>,
callback: Closure<FnMut(&Event)>,
options: &AddEventListenerOptions,
phase: EventListenerPhase,
) -> Self {
Expand Down Expand Up @@ -283,7 +283,7 @@ impl EventListener {
/// # use gloo_events::{EventListener, EventListenerOptions};
/// # let target = unimplemented!();
/// # let event_type = "click";
/// # let callback = move |e| {};
/// # fn callback(_: &web_sys::Event) {}
/// #
/// let options = EventListenerOptions::enable_prevent_default();
///
Expand All @@ -302,7 +302,7 @@ impl EventListener {
/// # use gloo_events::{EventListener, EventListenerOptions};
/// # let target = unimplemented!();
/// # let event_type = "click";
/// # let callback = move |e| {};
/// # fn callback(_: &web_sys::Event) {}
/// #
/// // This runs the event listener in the capture phase, rather than the bubble phase
/// let options = EventListenerOptions::run_in_capture_phase();
Expand All @@ -322,7 +322,7 @@ impl EventListener {
/// # let target = unimplemented!();
/// #
/// let listener = EventListener::new(&target, "click", move |event| {
/// let event = event.dyn_into::<web_sys::MouseEvent>().unwrap_throw();
/// let event = event.dyn_ref::<web_sys::MouseEvent>().unwrap_throw();
///
/// // ...
/// });
Expand All @@ -331,9 +331,9 @@ impl EventListener {
pub fn new<S, F>(target: &EventTarget, event_type: S, callback: F) -> Self
where
S: Into<Cow<'static, str>>,
F: FnMut(Event) + 'static,
F: FnMut(&Event) + 'static,
{
let callback = Closure::wrap(Box::new(callback) as Box<FnMut(Event)>);
let callback = Closure::wrap(Box::new(callback) as Box<FnMut(&Event)>);

NEW_OPTIONS.with(move |options| {
Self::raw_new(
Expand Down Expand Up @@ -362,7 +362,7 @@ impl EventListener {
/// # let target = unimplemented!();
/// #
/// let listener = EventListener::once(&target, "load", move |event| {
/// let event = event.dyn_into::<web_sys::ProgressEvent>().unwrap_throw();
/// let event = event.dyn_ref::<web_sys::ProgressEvent>().unwrap_throw();
///
/// // ...
/// });
Expand All @@ -371,7 +371,7 @@ impl EventListener {
pub fn once<S, F>(target: &EventTarget, event_type: S, callback: F) -> Self
where
S: Into<Cow<'static, str>>,
F: FnOnce(Event) + 'static,
F: FnOnce(&Event) + 'static,
{
let callback = Closure::once(callback);

Expand Down Expand Up @@ -450,9 +450,9 @@ impl EventListener {
) -> Self
where
S: Into<Cow<'static, str>>,
F: FnMut(Event) + 'static,
F: FnMut(&Event) + 'static,
{
let callback = Closure::wrap(Box::new(callback) as Box<FnMut(Event)>);
let callback = Closure::wrap(Box::new(callback) as Box<FnMut(&Event)>);

Self::raw_new(
target,
Expand Down Expand Up @@ -514,7 +514,7 @@ impl EventListener {
) -> Self
where
S: Into<Cow<'static, str>>,
F: FnOnce(Event) + 'static,
F: FnOnce(&Event) + 'static,
{
let callback = Closure::once(callback);

Expand Down Expand Up @@ -551,7 +551,7 @@ impl EventListener {

/// Returns the callback.
#[inline]
pub fn callback(&self) -> &Closure<FnMut(Event)> {
pub fn callback(&self) -> &Closure<FnMut(&Event)> {
// This will never panic, because `callback` is always `Some`
self.callback.as_ref().unwrap_throw()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/events/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn new_with_options() -> impl Future<Item = (), Error = JsValue> {
},
move |e| {
sender.send(|| {
is(e.dyn_into::<web_sys::MouseEvent>().is_ok(), true)?;
is(e.dyn_ref::<web_sys::MouseEvent>().is_some(), true)?;

Ok(())
});
Expand All @@ -111,7 +111,7 @@ fn once_with_options() -> impl Future<Item = (), Error = JsValue> {
},
move |e| {
sender.send(|| {
is(e.dyn_into::<web_sys::MouseEvent>().is_ok(), true)?;
is(e.dyn_ref::<web_sys::MouseEvent>().is_some(), true)?;

Ok(())
});
Expand All @@ -131,7 +131,7 @@ fn new() -> impl Future<Item = (), Error = JsValue> {

let _handler = EventListener::new(&body, "click", move |e| {
sender.send(|| {
is(e.dyn_into::<web_sys::MouseEvent>().is_ok(), true)?;
is(e.dyn_ref::<web_sys::MouseEvent>().is_some(), true)?;

Ok(())
});
Expand All @@ -150,7 +150,7 @@ fn once() -> impl Future<Item = (), Error = JsValue> {

let _handler = EventListener::once(&body, "click", move |e| {
sender.send(|| {
is(e.dyn_into::<web_sys::MouseEvent>().is_ok(), true)?;
is(e.dyn_ref::<web_sys::MouseEvent>().is_some(), true)?;

Ok(())
});
Expand Down