dioxus/packages/core/tests/suspend.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

2022-11-06 22:28:41 +00:00
use dioxus_core::*;
2022-11-09 03:39:37 +00:00
use std::{cell::RefCell, rc::Rc, time::Duration};
2022-11-06 22:28:41 +00:00
#[tokio::test]
async fn it_works() {
let mut dom = VirtualDom::new(app);
2022-11-09 03:39:37 +00:00
let mutations = dom.rebuild();
2022-11-06 22:28:41 +00:00
println!("mutations: {:?}", mutations);
dom.wait_for_work().await;
}
fn app(cx: Scope) -> Element {
2022-11-09 03:39:37 +00:00
println!("running root app");
2022-11-22 18:05:13 +00:00
VNode::template_from_dynamic_node(
2022-11-09 03:39:37 +00:00
cx,
cx.component(suspense_boundary, (), "suspense_boundary"),
"app",
)
}
fn suspense_boundary(cx: Scope) -> Element {
println!("running boundary");
let _ = cx.use_hook(|| {
cx.provide_context(Rc::new(RefCell::new(SuspenseBoundary::new(cx.scope_id()))))
});
2022-11-22 18:05:13 +00:00
VNode::template_from_dynamic_node(cx, cx.component(async_child, (), "async_child"), "app")
2022-11-06 22:28:41 +00:00
}
async fn async_child(cx: Scope<'_>) -> Element {
println!("rendering async child");
let fut = cx.use_hook(|| {
Box::pin(async {
println!("Starting sleep");
tokio::time::sleep(Duration::from_secs(1)).await;
println!("Sleep ended");
})
});
fut.await;
2022-11-08 06:55:22 +00:00
println!("Future awaited and complete");
2022-11-06 22:28:41 +00:00
2022-11-22 18:05:13 +00:00
VNode::template_from_dynamic_node(cx, cx.component(async_text, (), "async_text"), "app")
2022-11-09 03:39:37 +00:00
}
2022-11-08 06:55:22 +00:00
2022-11-09 03:39:37 +00:00
async fn async_text(cx: Scope<'_>) -> Element {
VNode::single_text(&cx, &[TemplateNode::Text("it works!")], "beauty")
2022-11-06 22:28:41 +00:00
}