Merge pull request #2005 from ealmloff/fix-suspense

Only poll suspended futures, lazy memos
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-03-07 23:43:52 -08:00
commit 295c29db5d
25 files changed
+519 -286

No files matched your search

+28 -3
View File
@@ -1,9 +1,10 @@
use dioxus::prelude::*;
use std::future::poll_fn;
use std::task::Poll;
#[test]
fn suspense_resolves() {
// wait just a moment, not enough time for the boundary to resolve
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
@@ -31,11 +32,35 @@ fn app() -> Element {
fn suspended_child() -> Element {
let mut val = use_signal(|| 0);
// Tasks that are not suspended should never be polled
spawn(async move {
panic!("Non-suspended task was polled");
});
// Memos should still work like normal
let memo = use_memo(move || val * 2);
assert_eq!(memo, val * 2);
if val() < 3 {
spawn(async move {
let task = spawn(async move {
// Poll each task 3 times
let mut count = 0;
poll_fn(|cx| {
println!("polling... {}", count);
if count < 3 {
count += 1;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
})
.await;
println!("waiting... {}", val);
val += 1;
});
suspend()?;
suspend(task)?;
}
rsx!("child")
+2 -2
View File
@@ -53,7 +53,7 @@ async fn running_async() {
#[tokio::test]
async fn yield_now_works() {
thread_local! {
static SEQUENCE: std::cell::RefCell<Vec<usize>> = std::cell::RefCell::new(Vec::new());
static SEQUENCE: std::cell::RefCell<Vec<usize>> = const { std::cell::RefCell::new(Vec::new()) };
}
fn app() -> Element {
@@ -88,7 +88,7 @@ async fn yield_now_works() {
#[tokio::test]
async fn flushing() {
thread_local! {
static SEQUENCE: std::cell::RefCell<Vec<usize>> = std::cell::RefCell::new(Vec::new());
static SEQUENCE: std::cell::RefCell<Vec<usize>> = const { std::cell::RefCell::new(Vec::new()) };
static BROADCAST: (tokio::sync::broadcast::Sender<()>, tokio::sync::broadcast::Receiver<()>) = tokio::sync::broadcast::channel(1);
}