Move event return to HTML crate

This commit is contained in:
Jonathan Kelley committed 2023-02-08 15:51:28 -08:00
1 parent ffb2b6f4de
commit a6c13c8ad0
4 files changed
+34 -30

No files matched your search

+26 -2
View File
@@ -8,10 +8,13 @@ macro_rules! impl_event {
) => {
$(
$( #[$attr] )*
pub fn $name<'a, E: ::dioxus_core::EventReturn<T>, T>(_cx: &'a ::dioxus_core::ScopeState, _f: impl FnMut(::dioxus_core::Event<$data>) -> E + 'a) -> ::dioxus_core::Attribute<'a> {
#[inline]
pub fn $name<'a, E: crate::EventReturn<T>, T>(_cx: &'a ::dioxus_core::ScopeState, mut _f: impl FnMut(::dioxus_core::Event<$data>) -> E + 'a) -> ::dioxus_core::Attribute<'a> {
::dioxus_core::Attribute {
name: stringify!($name),
value: _cx.listener(_f),
value: _cx.listener(move |e: ::dioxus_core::Event<$data>| {
_f(e).spawn(_cx);
}),
namespace: None,
mounted_element: Default::default(),
volatile: false,
@@ -144,3 +147,24 @@ pub fn event_bubbles(evt: &str) -> bool {
_ => true,
}
}
use std::future::Future;
#[doc(hidden)]
pub trait EventReturn<P>: Sized {
fn spawn(self, _cx: &dioxus_core::ScopeState) {}
}
impl EventReturn<()> for () {}
#[doc(hidden)]
pub struct AsyncMarker;
impl<T> EventReturn<AsyncMarker> for T
where
T: Future<Output = ()> + 'static,
{
#[inline]
fn spawn(self, cx: &dioxus_core::ScopeState) {
cx.spawn(self);
}
}