Skip to content

Commit

Permalink
change: run cleanups based on reactive owner, not Scope (closes #802)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Jul 2, 2023
1 parent dda9518 commit 8a80e3e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion leptos_reactive/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,15 @@ impl Scope {
}
}

#[track_caller]
#[cfg_attr(
any(debug_assertions, features = "ssr"),
instrument(level = "trace", skip_all,)
)]
fn push_cleanup(
cx: Scope,
cleanup_fn: Box<dyn FnOnce()>,
#[cfg(debug_assertions)] defined_at: &'static std::panic::Location<'static>,
) {
_ = with_runtime(cx.runtime, |runtime| {
if let Some(owner) = runtime.owner.get() {
Expand All @@ -378,6 +380,12 @@ fn push_cleanup(
} else {
cleanups.insert(owner, vec![cleanup_fn]);
}
} else {
crate::macros::debug_warn!(
"At {defined_at}, you are calling `on_cleanup` outside the \
reactive graph. This means that this cleanup function will \
never be called."
);
}
});
}
Expand All @@ -387,8 +395,9 @@ fn push_cleanup(
/// It runs after child scopes have been disposed, but before signals, effects, and resources
/// are invalidated.
#[inline(always)]
#[track_caller]
pub fn on_cleanup(cx: Scope, cleanup_fn: impl FnOnce() + 'static) {
push_cleanup(cx, Box::new(cleanup_fn))
push_cleanup(cx, Box::new(cleanup_fn), #[cfg(debug_assertions)] std::panic::Location::caller())
}

slotmap::new_key_type! {
Expand Down

0 comments on commit 8a80e3e

Please sign in to comment.