dioxus/packages/core/tests/suspense.rs

101 lines
3.1 KiB
Rust
Raw Normal View History

2022-11-24 07:15:01 +00:00
use dioxus::core::ElementId;
2022-12-03 00:24:49 +00:00
use dioxus::core::{Mutation::*, SuspenseContext};
2022-11-24 07:15:01 +00:00
use dioxus::prelude::*;
2022-11-27 05:56:49 +00:00
use std::future::IntoFuture;
2022-12-07 01:41:47 +00:00
use std::rc::Rc;
2022-12-05 21:03:52 +00:00
use std::time::Duration;
2022-11-06 22:28:41 +00:00
2023-01-09 21:50:33 +00:00
#[test]
fn it_works() {
2022-11-24 07:15:01 +00:00
// wait just a moment, not enough time for the boundary to resolve
2022-11-06 22:28:41 +00:00
2023-01-09 21:50:33 +00:00
tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap()
.block_on(async {
let mut dom = VirtualDom::new(app);
{
let mutations = dom.rebuild().santize();
// We should at least get the top-level template in before pausing for the children
// note: we dont test template edits anymore
// assert_eq!(
// mutations.templates,
// [
// CreateElement { name: "div" },
// CreateStaticText { value: "Waiting for child..." },
// CreateStaticPlaceholder,
// AppendChildren { m: 2 },
// SaveTemplate { name: "template", m: 1 }
// ]
// );
// And we should load it in and assign the placeholder properly
assert_eq!(
mutations.edits,
[
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
// hmmmmmmmmm.... with suspense how do we guarantee that IDs increase linearly?
// can we even?
AssignId { path: &[1], id: ElementId(3) },
AppendChildren { m: 1, id: ElementId(0) },
]
);
}
dom.wait_for_work().await;
});
2022-11-06 22:28:41 +00:00
}
fn app(cx: Scope) -> Element {
2022-11-24 07:15:01 +00:00
cx.render(rsx!(
div {
"Waiting for child..."
suspense_boundary {}
}
))
2022-11-09 03:39:37 +00:00
}
fn suspense_boundary(cx: Scope) -> Element {
2022-12-05 21:03:52 +00:00
cx.use_hook(|| {
2022-12-07 01:41:47 +00:00
cx.provide_context(Rc::new(SuspenseContext::new(cx.scope_id())));
2022-12-05 21:03:52 +00:00
});
2022-11-09 03:39:37 +00:00
2022-11-24 07:15:01 +00:00
// Ensure the right types are found
2022-12-07 01:41:47 +00:00
cx.has_context::<Rc<SuspenseContext>>().unwrap();
2022-11-09 03:39:37 +00:00
2022-11-24 07:15:01 +00:00
cx.render(rsx!(async_child {}))
2022-11-06 22:28:41 +00:00
}
async fn async_child(cx: Scope<'_>) -> Element {
2022-11-24 07:15:01 +00:00
use_future!(cx, || tokio::time::sleep(Duration::from_millis(10))).await;
cx.render(rsx!(async_text {}))
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 {
2022-11-27 05:56:49 +00:00
let username = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"async child 1"
});
let age = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
1234
});
let (_user, _age) = use_future!(cx, || async {
tokio::join!(
tokio::time::sleep(std::time::Duration::from_secs(1)),
tokio::time::sleep(std::time::Duration::from_secs(2))
);
("async child 1", 1234)
})
.await;
let (username, age) = tokio::join!(username.into_future(), age.into_future());
cx.render(rsx!( div { "Hello! {username}, you are {age}, {_user} {_age}" } ))
2022-11-06 22:28:41 +00:00
}