Skip to content

Commit

Permalink
Deduplicate suspensions when considering subsequent renders for suspe…
Browse files Browse the repository at this point in the history
…nsions (#3099)

* FIx duplicate suspension.

* Fix tests.

* Fix tests.

* Fix tests.
  • Loading branch information
futursolo committed Feb 1, 2023
1 parent 456a05b commit 65b930a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/yew/src/suspense/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ mod feat_csr_ssr {
return false;
}

// If a suspension already exists, ignore it.
if self.suspensions.iter().any(|n| n == &m) {
return false;
}

self.suspensions.push(m);

true
Expand Down
37 changes: 37 additions & 0 deletions packages/yew/tests/suspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,40 @@ async fn resume_after_unmount() {
let result = obtain_result();
assert_eq!(result.as_str(), "<div>Content replacement</div>");
}

#[wasm_bindgen_test]
async fn test_duplicate_suspension() {
use yew::html::ChildrenProps;

#[function_component]
fn FetchingProvider(props: &ChildrenProps) -> HtmlResult {
use_future(|| async {
sleep(Duration::ZERO).await;
})?;
Ok(html! { <>{props.children.clone()}</> })
}

#[function_component]
fn Child() -> Html {
html! {<div id="result">{"hello!"}</div>}
}

#[function_component]
fn App() -> Html {
let fallback = Html::default();
html! {
<Suspense {fallback}>
<FetchingProvider>
<Child />
</FetchingProvider>
</Suspense>
}
}

yew::Renderer::<App>::with_root(gloo::utils::document().get_element_by_id("output").unwrap())
.render();

sleep(Duration::from_millis(50)).await;
let result = obtain_result();
assert_eq!(result.as_str(), "hello!");
}

0 comments on commit 65b930a

Please sign in to comment.