wip: add use_callback hook

This commit is contained in:
Jonathan Kelley committed 2022-11-19 13:21:02 -08:00
1 parent 6c677e64da
commit 57c10174ec
4 files changed
+62 -6

No files matched your search

+2 -2
View File
@@ -19,5 +19,5 @@ pub use usefuture::*;
mod useeffect;
pub use useeffect::*;
// mod usesuspense;
// pub use usesuspense::*;
mod usecallback;
pub use usecallback::*;
+36
View File
@@ -0,0 +1,36 @@
use std::rc::Rc;
use dioxus_core::{prelude::EventHandler, ScopeState};
use std::future::Future;
#[macro_export]
macro_rules! use_callback {
($cx:ident, || || $($rest:tt)*) => { use_callback( $cx, (), |_| $($rest)* ) };
($cx:ident, || |$myarg:ident| $($rest:tt)*) => {
use_callback(
$cx,
|| |$myarg| async {}
)
};
}
pub fn use_callback<'a, T, R, F>(cx: &'a ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + 'a
where
R: FnMut(T) -> F + 'static,
F: Future<Output = ()> + 'static,
{
let mut hook = make();
move |evt| cx.spawn(hook(evt))
}
fn it_works(cx: &ScopeState) {
let p = use_callback(cx, || {
|()| async {
//
}
});
// let p = use_callback!(cx, || |evt| async {
// //
// });
}