change: add window_event_listener_untyped and deprecate window_event_listener pending 0.3.0 (#913)

This commit is contained in:
Greg Johnston 2023-04-21 14:14:35 -04:00 committed by GitHub
parent d3a577c365
commit f3b8d27c4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 4 deletions

View file

@ -160,7 +160,8 @@ pub use leptos_dom::{
request_animation_frame, request_animation_frame_with_handle, request_animation_frame, request_animation_frame_with_handle,
request_idle_callback, request_idle_callback_with_handle, set_interval, request_idle_callback, request_idle_callback_with_handle, set_interval,
set_interval_with_handle, set_timeout, set_timeout_with_handle, set_interval_with_handle, set_timeout, set_timeout_with_handle,
window_event_listener, window_event_listener, window_event_listener_untyped,
window_event_listener_with_precast,
}, },
html, log, math, mount_to, mount_to_body, svg, warn, window, Attribute, html, log, math, mount_to, mount_to_body, svg, warn, window, Attribute,
Class, Errors, Fragment, HtmlElement, IntoAttribute, IntoClass, Class, Errors, Fragment, HtmlElement, IntoAttribute, IntoClass,

View file

@ -408,9 +408,27 @@ pub fn set_interval_with_handle(
instrument(level = "trace", skip_all, fields(event_name = %event_name)) instrument(level = "trace", skip_all, fields(event_name = %event_name))
)] )]
#[inline(always)] #[inline(always)]
#[deprecated = "In the next release, `window_event_listener` will become \
typed. You can switch now to `window_event_listener_untyped` \
for the current behavior or use \
`window_event_listener_with_precast`, which will become the \
new`window_event_listener`."]
pub fn window_event_listener( pub fn window_event_listener(
event_name: &str, event_name: &str,
cb: impl Fn(web_sys::Event) + 'static, cb: impl Fn(web_sys::Event) + 'static,
) {
window_event_listener_untyped(event_name, cb)
}
/// Adds an event listener to the `Window`, typed as a generic `Event`.
#[cfg_attr(
debug_assertions,
instrument(level = "trace", skip_all, fields(event_name = %event_name))
)]
#[inline(always)]
pub fn window_event_listener_untyped(
event_name: &str,
cb: impl Fn(web_sys::Event) + 'static,
) { ) {
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(debug_assertions)] { if #[cfg(debug_assertions)] {
@ -445,7 +463,7 @@ pub fn window_event_listener_with_precast<E: ev::EventDescriptor + 'static>(
) where ) where
E::EventType: JsCast, E::EventType: JsCast,
{ {
window_event_listener(&event.name(), move |e| { window_event_listener_untyped(&event.name(), move |e| {
cb(e.unchecked_into::<E::EventType>()) cb(e.unchecked_into::<E::EventType>())
}); });
} }

View file

@ -169,7 +169,7 @@ impl RouterContext {
// handle all click events on anchor tags // handle all click events on anchor tags
#[cfg(not(feature = "ssr"))] #[cfg(not(feature = "ssr"))]
leptos::window_event_listener("click", { leptos::window_event_listener_untyped("click", {
let inner = Rc::clone(&inner); let inner = Rc::clone(&inner);
move |ev| inner.clone().handle_anchor_click(ev) move |ev| inner.clone().handle_anchor_click(ev)
}); });

View file

@ -55,7 +55,7 @@ impl History for BrowserIntegration {
let (location, set_location) = create_signal(cx, Self::current(false)); let (location, set_location) = create_signal(cx, Self::current(false));
leptos::window_event_listener("popstate", move |_| { leptos::window_event_listener_untyped("popstate", move |_| {
let router = use_context::<RouterContext>(cx); let router = use_context::<RouterContext>(cx);
if let Some(router) = router { if let Some(router) = router {
let is_back = router.inner.is_back; let is_back = router.inner.is_back;