suspend.rs 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use std::{cell::Cell, ptr::null_mut, time::Duration};
  2. use dioxus_core::*;
  3. #[tokio::test]
  4. async fn it_works() {
  5. let mut dom = VirtualDom::new(app);
  6. let mut mutations = vec![];
  7. dom.rebuild(&mut mutations);
  8. println!("mutations: {:?}", mutations);
  9. dom.wait_for_work().await;
  10. }
  11. fn app(cx: Scope) -> Element {
  12. let dy = cx.component(async_child, (), "async_child");
  13. VNode::single_component(&cx, dy, "app")
  14. }
  15. async fn async_child(cx: Scope<'_>) -> Element {
  16. println!("rendering async child");
  17. let fut = cx.use_hook(|| {
  18. Box::pin(async {
  19. println!("Starting sleep");
  20. tokio::time::sleep(Duration::from_secs(1)).await;
  21. println!("Sleep ended");
  22. })
  23. });
  24. fut.await;
  25. println!("Future awaited and complete");
  26. let dy = cx.component(async_child, (), "async_child");
  27. VNode::single_component(&cx, dy, "app")
  28. // VNode::single_text(&cx, &[TemplateNode::Text("it works!")], "beauty")
  29. }