summary refs log tree commit diff stats
path: root/src/components/async_fetch.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/components/async_fetch.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/components/async_fetch.rs b/src/components/async_fetch.rs
new file mode 100644
index 0000000..7105c6f
--- /dev/null
+++ b/src/components/async_fetch.rs
@@ -0,0 +1,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>
+//     }
+// }