usecallback.rs 811 B

123456789101112131415161718192021222324252627282930313233343536
  1. use std::rc::Rc;
  2. use dioxus_core::{prelude::EventHandler, ScopeState};
  3. use std::future::Future;
  4. #[macro_export]
  5. macro_rules! use_callback {
  6. ($cx:ident, || || $($rest:tt)*) => { use_callback( $cx, (), |_| $($rest)* ) };
  7. ($cx:ident, || |$myarg:ident| $($rest:tt)*) => {
  8. use_callback(
  9. $cx,
  10. || |$myarg| async {}
  11. )
  12. };
  13. }
  14. pub fn use_callback<'a, T, R, F>(cx: &'a ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + 'a
  15. where
  16. R: FnMut(T) -> F + 'static,
  17. F: Future<Output = ()> + 'static,
  18. {
  19. let mut hook = make();
  20. move |evt| cx.spawn(hook(evt))
  21. }
  22. fn it_works(cx: &ScopeState) {
  23. let p = use_callback(cx, || {
  24. |()| async {
  25. //
  26. }
  27. });
  28. // let p = use_callback!(cx, || |evt| async {
  29. // //
  30. // });
  31. }