suspense.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use std::future::IntoFuture;
  2. use dioxus::prelude::*;
  3. use dioxus_core::SuspenseBoundary;
  4. #[inline_props]
  5. fn suspense_boundary<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
  6. cx.use_hook(|| cx.provide_context(SuspenseBoundary::new(cx.scope_id())));
  7. cx.render(rsx! { children })
  8. }
  9. fn basic_child(cx: Scope) -> Element {
  10. cx.render(rsx! {
  11. div { "basic child 1" }
  12. })
  13. }
  14. async fn async_child(cx: Scope<'_>) -> Element {
  15. let username = use_future!(cx, || async {
  16. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  17. "async child 1"
  18. });
  19. let age = use_future!(cx, || async {
  20. tokio::time::sleep(std::time::Duration::from_secs(2)).await;
  21. println!("long future completed");
  22. 1234
  23. });
  24. let (_user, _age) = use_future!(cx, || async {
  25. tokio::join!(
  26. tokio::time::sleep(std::time::Duration::from_secs(1)),
  27. tokio::time::sleep(std::time::Duration::from_secs(2))
  28. );
  29. ("async child 1", 1234)
  30. })
  31. .await;
  32. let (username, age) = tokio::join!(username.into_future(), age.into_future());
  33. cx.render(rsx!(
  34. div { "Hello! {username}, you are {age}, {_user} {_age}" }
  35. ))
  36. }
  37. #[tokio::test]
  38. async fn basic_prints() {
  39. let mut dom = VirtualDom::new(|cx| {
  40. cx.render(rsx! {
  41. div {
  42. h1 { "var" }
  43. suspense_boundary {
  44. basic_child { }
  45. async_child { }
  46. }
  47. }
  48. })
  49. });
  50. dbg!(dom.rebuild());
  51. dom.wait_for_work().await;
  52. dbg!(dom.rebuild());
  53. }