1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use dioxus_core::ScopeState;
use std::future::Future;

#[macro_export]
macro_rules! use_callback {
    // ($cx:ident, || || $($rest:tt)*) => { use_callback( $cx, (), |_| $($rest)* ) };
    // ($cx:ident, || || $($rest:tt)*) => { use_callback( $cx, (), |_| $($rest)* ) };
    ($cx:ident, || $($rest:tt)*) => {
        use_callback(
            $cx,
            move || $($rest)*
        )
    };
    ($cx:ident, |$($args:tt),* | $($rest:tt)*) => {
        use_callback(
            $cx,
            move || $($rest)*
        )
    };
    ($cx:ident, $($rest:tt)*) => {
        use_callback(
            $cx,
            move || $($rest)*
        )
    };
}

pub fn use_callback<T, R, F>(cx: &ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + '_
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 {
    //     //
    // });
}