1
0
Jonathan Kelley 3 жил өмнө
parent
commit
7a5c403e38

+ 2 - 2
packages/hooks/src/lib.rs

@@ -13,8 +13,8 @@ pub use usecoroutine::*;
 mod usefuture;
 pub use usefuture::*;
 
-mod usesuspense;
-pub use usesuspense::*;
+mod usecallback;
+pub use usecallback::*;
 
 #[macro_export]
 /// A helper macro for using hooks in async environements.

+ 68 - 0
packages/hooks/src/usecallback.rs

@@ -0,0 +1,68 @@
+use std::{cell::RefCell, rc::Rc};
+
+use crate::use_state;
+use crate::UseFutureDep;
+use dioxus_core::{ScopeState, UiEvent};
+use std::future::Future;
+
+pub fn use_callback<I, G: UseFutureDep, F: Future<Output = ()> + 'static>(
+    cx: &ScopeState,
+    //
+    g: G,
+    f: impl FnMut(I, G::Out) -> F,
+) -> &UseCallback<I, G::Out>
+where
+    G::Out: 'static,
+    I: 'static,
+{
+    cx.use_hook(|_| {
+        //
+        UseCallback {
+            f: todo!(),
+            f2: Box::new(|f| {}),
+        }
+    })
+}
+
+pub struct UseCallback<I, T> {
+    f: Rc<RefCell<Option<Box<dyn FnMut(I, T)>>>>,
+    f2: Box<dyn Fn(I)>, // f: Rc<RefCell<Option<Box<dyn FnMut(I, T)>>>>,
+}
+
+impl<I: 'static, T> std::ops::Deref for UseCallback<I, T> {
+    type Target = dyn Fn(I);
+
+    fn deref(&self) -> &Self::Target {
+        &self.f2
+    }
+}
+
+trait MyThing {}
+impl<A> MyThing for Box<dyn Fn(A)> {}
+impl<A, B> MyThing for Box<dyn Fn(A, B)> {}
+
+#[test]
+fn demo() {
+    use dioxus_core::prelude::*;
+    fn example(cx: Scope) -> Element {
+        let (name, _) = use_state(&cx, || 0);
+        let (age, _) = use_state(&cx, || 0);
+
+        let onsubmit = use_callback(&cx, (name,), |event: (), (name,)| async move {
+            //
+        });
+
+        let onsubmit = use_callback(&cx, (name,), my_callback);
+        async fn my_callback(event: UiEvent<()>, name: (i32,)) {
+            //
+        }
+
+        let onsubmit = use_callback(&cx, name, my_callback2);
+
+        async fn my_callback2(event: UiEvent<()>, name: i32) {
+            //
+        }
+
+        None
+    }
+}

+ 22 - 0
packages/hooks/src/usefuture.rs

@@ -146,6 +146,28 @@ impl UseFutureDep for () {
 pub trait Dep: 'static + PartialEq + Clone {}
 impl<T> Dep for T where T: 'static + PartialEq + Clone {}
 
+impl<A: Dep> UseFutureDep for &A {
+    type Out = A;
+    fn out(&self) -> Self::Out {
+        (*self).clone()
+    }
+    fn apply(self, state: &mut Vec<Box<dyn Any>>) -> bool {
+        match state.get_mut(0).map(|f| f.downcast_mut::<A>()).flatten() {
+            Some(val) => {
+                if *val != *self {
+                    *val = self.clone();
+                    return true;
+                }
+            }
+            None => {
+                state.push(Box::new(self.clone()));
+                return true;
+            }
+        }
+        false
+    }
+}
+
 macro_rules! impl_dep {
     (
         $($el:ident=$name:ident,)*