summary refs log tree commit diff stats
path: root/src/components/async_fetch.rs
blob: 7105c6f4a89b80e062d6b3051c548fc990a8c241 (plain) (blame)
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
49
50
macro_rules! AsyncFetch {
    (fetcher = $fetcher:block producer = |$bound_variable:pat_param| $producer:block) => {{
        use leptos::{
            prelude::{ElementChild, LocalResource, Suspend, Transition},
            view,
        };

        view! {
            <Transition fallback=|| {
                view! { <p>"Loading..."</p> }
            }>
                {move || Suspend::new(async move {
                    let resource = { LocalResource::new(move || $fetcher) };
                    resource
                        .await
                        .map(|$bound_variable| $producer)
                })}
            </Transition>
        }
    }};
}
pub(crate) use AsyncFetch;

// #[component]
// pub fn AsyncFetch<P, V, T, Fut>(
//     fetcher: impl Fn() -> Fut + 'static + Send + Sync,
//     producer: P,
// ) -> impl IntoView
// where
//     V: IntoView + 'static,
//     P: Fn(T) -> V + 'static + Send + Sync,
//     Fut: Future<Output = Result<T, Error>> + 'static,
//     T: 'static,
//     LocalResource<Result<T, Error>>: IntoFuture<Output = Result<T, Error>> + Send,
// {
//     view! {
//             <Transition fallback=|| {
//                 view! { <p>"Loading..."</p> }
//             }>
//                 { || Suspend::new(async {
//                     let value_resource = LocalResource::new( || fetcher());
//                     value_resource
//                         .await
//                         .map(|value| {
//                             producer(value)
//                         })
//                 })}
//             </Transition>
//     }
// }