Quellcode durchsuchen

Move event return to HTML crate

Jonathan Kelley vor 2 Jahren
Ursprung
Commit
a6c13c8ad0
4 geänderte Dateien mit 34 neuen und 30 gelöschten Zeilen
  1. 0 19
      packages/core/src/events.rs
  2. 4 4
      packages/core/src/lib.rs
  3. 4 5
      packages/core/src/scopes.rs
  4. 26 2
      packages/html/src/events.rs

+ 0 - 19
packages/core/src/events.rs

@@ -3,10 +3,6 @@ use std::{
     rc::Rc,
 };
 
-use futures_util::Future;
-
-use crate::ScopeState;
-
 /// A wrapper around some generic data that handles the event's state
 ///
 ///
@@ -111,21 +107,6 @@ impl<T: std::fmt::Debug> std::fmt::Debug for Event<T> {
 }
 
 #[doc(hidden)]
-pub trait EventReturn<P>: Sized {
-    fn spawn(self, _cx: &ScopeState) {}
-}
-
-impl EventReturn<()> for () {}
-
-pub struct AsyncMarker;
-impl<T> EventReturn<AsyncMarker> for T
-where
-    T: Future<Output = ()> + 'static,
-{
-    fn spawn(self, cx: &ScopeState) {
-        cx.spawn(self);
-    }
-}
 
 /// The callback type generated by the `rsx!` macro when an `on` field is specified for components.
 ///

+ 4 - 4
packages/core/src/lib.rs

@@ -71,10 +71,10 @@ pub(crate) mod innerlude {
 
 pub use crate::innerlude::{
     fc_to_builder, AnyValue, Attribute, AttributeValue, BorrowedAttributeValue, CapturedError,
-    Component, DynamicNode, Element, ElementId, Event, EventReturn, Fragment, IntoDynNode,
-    LazyNodes, Mutation, Mutations, Properties, RenderReturn, Scope, ScopeId, ScopeState, Scoped,
-    SuspenseContext, TaskId, Template, TemplateAttribute, TemplateNode, VComponent, VNode,
-    VPlaceholder, VText, VirtualDom,
+    Component, DynamicNode, Element, ElementId, Event, Fragment, IntoDynNode, LazyNodes, Mutation,
+    Mutations, Properties, RenderReturn, Scope, ScopeId, ScopeState, Scoped, SuspenseContext,
+    TaskId, Template, TemplateAttribute, TemplateNode, VComponent, VNode, VPlaceholder, VText,
+    VirtualDom,
 };
 
 /// The purpose of this module is to alleviate imports of many common types

+ 4 - 5
packages/core/src/scopes.rs

@@ -7,7 +7,7 @@ use crate::{
     innerlude::{ErrorBoundary, Scheduler, SchedulerMsg},
     lazynodes::LazyNodes,
     nodes::{ComponentReturn, IntoAttributeValue, IntoDynNode, RenderReturn},
-    AnyValue, Attribute, AttributeValue, Element, Event, EventReturn, Properties, TaskId,
+    AnyValue, Attribute, AttributeValue, Element, Event, Properties, TaskId,
 };
 use bumpalo::{boxed::Box as BumpBox, Bump};
 use bumpslab::{BumpSlab, Slot};
@@ -581,9 +581,9 @@ impl<'src> ScopeState {
     /// Create a new [`AttributeValue`] with the listener variant from a callback
     ///
     /// The callback must be confined to the lifetime of the ScopeState
-    pub fn listener<T: 'static, P, E: EventReturn<P>>(
+    pub fn listener<T: 'static>(
         &'src self,
-        mut callback: impl FnMut(Event<T>) -> E + 'src,
+        mut callback: impl FnMut(Event<T>) + 'src,
     ) -> AttributeValue<'src> {
         // safety: there's no other way to create a dynamicly-dispatched bump box other than alloc + from-raw
         // This is the suggested way to build a bumpbox
@@ -595,8 +595,7 @@ impl<'src> ScopeState {
                     callback(Event {
                         propagates: event.propagates,
                         data,
-                    })
-                    .spawn(self);
+                    });
                 }
             }))
         };

+ 26 - 2
packages/html/src/events.rs

@@ -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);
+    }
+}