suspend.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use dioxus_core::*;
  2. use std::{cell::RefCell, rc::Rc, time::Duration};
  3. #[tokio::test]
  4. async fn it_works() {
  5. let mut dom = VirtualDom::new(app);
  6. let mutations = dom.rebuild();
  7. println!("mutations: {:?}", mutations);
  8. dom.wait_for_work().await;
  9. }
  10. fn app(cx: Scope) -> Element {
  11. println!("running root app");
  12. VNode::template_from_dynamic_node(
  13. cx,
  14. cx.component(suspense_boundary, (), "suspense_boundary"),
  15. "app",
  16. )
  17. }
  18. fn suspense_boundary(cx: Scope) -> Element {
  19. println!("running boundary");
  20. let _ = cx.use_hook(|| {
  21. cx.provide_context(Rc::new(RefCell::new(SuspenseBoundary::new(cx.scope_id()))))
  22. });
  23. VNode::template_from_dynamic_node(cx, cx.component(async_child, (), "async_child"), "app")
  24. }
  25. async fn async_child(cx: Scope<'_>) -> Element {
  26. println!("rendering async child");
  27. let fut = cx.use_hook(|| {
  28. Box::pin(async {
  29. println!("Starting sleep");
  30. tokio::time::sleep(Duration::from_secs(1)).await;
  31. println!("Sleep ended");
  32. })
  33. });
  34. fut.await;
  35. println!("Future awaited and complete");
  36. VNode::template_from_dynamic_node(cx, cx.component(async_text, (), "async_text"), "app")
  37. }
  38. async fn async_text(cx: Scope<'_>) -> Element {
  39. VNode::single_text(&cx, &[TemplateNode::Text("it works!")], "beauty")
  40. }