suspense.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //! Example: Suspense
  2. //! -----------------
  3. //! This example shows how the "use_fetch" hook is built on top of Dioxus' "suspense" API. Suspense enables components
  4. //! to wait on futures to complete before rendering the result into VNodes. These VNodes are immediately available in a
  5. //! "suspended" fashion and will automatically propogate to the UI when the future completes.
  6. //!
  7. //! Note that if your component updates or receives new props while it is awating the result, the future will be dropped
  8. //! and all work will stop. In this example, we store the future in a hook so we can always resume it.
  9. use dioxus::prelude::*;
  10. fn main() {}
  11. #[derive(serde::Deserialize)]
  12. struct DogApi {
  13. message: String,
  14. }
  15. const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
  16. pub static App: FC<()> = |cx| {
  17. let doggo = use_future_effect(cx, move || async move {
  18. match surf::get(ENDPOINT).recv_json::<DogApi>().await {
  19. Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
  20. Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
  21. }
  22. });
  23. cx.render(rsx!(
  24. div {
  25. h1 {"Waiting for a doggo..."}
  26. {doggo}
  27. }
  28. ))
  29. };
  30. // use dioxus_core::virtual_dom::SuspendedContext;
  31. use futures::Future;
  32. use futures::FutureExt;
  33. use std::pin::Pin;
  34. fn use_fetch<'a, T, P, F>(cx: Context<P>, url: &str, g: F) -> VNode<'a>
  35. where
  36. T: serde::de::DeserializeOwned + 'static,
  37. // F: FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
  38. F: FnOnce((), surf::Result<T>) -> VNode<'a> + 'a,
  39. {
  40. todo!()
  41. // // essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
  42. // let fetch_promise = cx.use_hook(
  43. // move || surf::get(url).recv_json::<T>().boxed_local(),
  44. // // just pass the future through
  45. // |p| p,
  46. // |_| (),
  47. // );
  48. // cx.suspend(fetch_promise, g)
  49. }
  50. /// Spawns the future only when the inputs change
  51. fn use_future_effect<'a, 'b, F, P, F2>(cx: Context<P>, g: F2) -> VNode<'a>
  52. where
  53. F: Future<Output = VNode<'b>>,
  54. F2: FnOnce() -> F + 'a,
  55. {
  56. todo!()
  57. }