fix: properly dispose of <Suspense/> scopes (closes #834) (#1006)

This commit is contained in:
Greg Johnston 2023-05-05 19:08:56 -04:00 committed by GitHub
parent a5f6e0bac4
commit b5e94e4054
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,11 @@
use cfg_if::cfg_if;
use leptos_dom::{DynChild, Fragment, HydrationCtx, IntoView};
use leptos_macro::component;
#[cfg(any(feature = "csr", feature = "hydrate"))]
use leptos_reactive::ScopeDisposer;
use leptos_reactive::{provide_context, Scope, SuspenseContext};
#[cfg(any(feature = "csr", feature = "hydrate"))]
use std::cell::RefCell;
use std::rc::Rc;
/// If any [Resources](leptos_reactive::Resource) are read in the `children` of this
@ -72,6 +76,8 @@ where
let before_me = HydrationCtx::peek();
let current_id = HydrationCtx::next_component();
#[cfg(any(feature = "csr", feature = "hydrate"))]
let prev_disposer = Rc::new(RefCell::new(None::<ScopeDisposer>));
let child = DynChild::new({
#[cfg(not(any(feature = "csr", feature = "hydrate")))]
@ -79,11 +85,18 @@ where
move || {
cfg_if! {
if #[cfg(any(feature = "csr", feature = "hydrate"))] {
if context.ready() {
if let Some(disposer) = prev_disposer.take() {
disposer.dispose();
}
let (view, disposer) =
cx.run_child_scope(|cx| if context.ready() {
Fragment::lazy(Box::new(|| vec![orig_child(cx).into_view(cx)])).into_view(cx)
} else {
Fragment::lazy(Box::new(|| vec![fallback().into_view(cx)])).into_view(cx)
}
});
*prev_disposer.borrow_mut() = Some(disposer);
view
} else {
use leptos_reactive::signal_prelude::*;