suspense.rs 1.6 KB

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