From ec4bd7600f0877d6d0b2b49b5b7fe4421dc06a8b Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Wed, 23 Aug 2023 20:57:05 -0400 Subject: [PATCH] fix: suppress warning about non-reactivity when calling `.refetch()` (occurs in e.g., async blocks in actions) (#1576) * fix: suppress warning about non-reactivity when calling `.refetch()` (occurs in e.g., async blocks in actions) * fix: don't reenter reactivity if these are nested --- leptos_dom/src/events.rs | 8 ++++---- leptos_dom/src/helpers.rs | 16 ++++++++-------- leptos_reactive/src/diagnostics.rs | 20 ++++++++++---------- leptos_reactive/src/resource.rs | 10 ++++++++-- leptos_reactive/src/runtime.rs | 20 ++++++++++++++------ 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/leptos_dom/src/events.rs b/leptos_dom/src/events.rs index a8af5854bc..e951ebcb68 100644 --- a/leptos_dom/src/events.rs +++ b/leptos_dom/src/events.rs @@ -59,10 +59,10 @@ pub fn add_event_listener( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = Box::new(move |e| { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(e); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }); } } @@ -88,10 +88,10 @@ pub(crate) fn add_event_listener_undelegated( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = Box::new(move |e| { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(e); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }); } } diff --git a/leptos_dom/src/helpers.rs b/leptos_dom/src/helpers.rs index 6ab4237b2e..3f8eefb64d 100644 --- a/leptos_dom/src/helpers.rs +++ b/leptos_dom/src/helpers.rs @@ -218,10 +218,10 @@ pub fn set_timeout_with_handle( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = move || { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }; } } @@ -273,10 +273,10 @@ pub fn debounce( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = move |value| { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(value); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }; } } @@ -351,10 +351,10 @@ pub fn set_interval_with_handle( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = move || { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }; } } @@ -392,10 +392,10 @@ pub fn window_event_listener_untyped( if #[cfg(debug_assertions)] { let span = ::tracing::Span::current(); let cb = move |e| { - leptos_reactive::SpecialNonReactiveZone::enter(); + let prev = leptos_reactive::SpecialNonReactiveZone::enter(); let _guard = span.enter(); cb(e); - leptos_reactive::SpecialNonReactiveZone::exit(); + leptos_reactive::SpecialNonReactiveZone::exit(prev); }; } } diff --git a/leptos_reactive/src/diagnostics.rs b/leptos_reactive/src/diagnostics.rs index c458ec7b09..d026592b51 100644 --- a/leptos_reactive/src/diagnostics.rs +++ b/leptos_reactive/src/diagnostics.rs @@ -43,18 +43,18 @@ impl SpecialNonReactiveZone { false } - #[inline(always)] - pub fn enter() { - #[cfg(debug_assertions)] - { - IS_SPECIAL_ZONE.with(|val| val.set(true)) - } + #[cfg(debug_assertions)] + pub fn enter() -> bool { + IS_SPECIAL_ZONE.with(|val| { + let prev = val.get(); + val.set(true); + prev + }) } - #[inline(always)] - pub fn exit() { - #[cfg(debug_assertions)] - { + #[cfg(debug_assertions)] + pub fn exit(prev: bool) { + if !prev { IS_SPECIAL_ZONE.with(|val| val.set(false)) } } diff --git a/leptos_reactive/src/resource.rs b/leptos_reactive/src/resource.rs index 7d7f0fb61c..7441d2c3a4 100644 --- a/leptos_reactive/src/resource.rs +++ b/leptos_reactive/src/resource.rs @@ -4,7 +4,7 @@ use crate::{ signal_prelude::format_signal_warning, spawn::spawn_local, use_context, GlobalSuspenseContext, Memo, ReadSignal, ScopeProperty, SignalDispose, SignalGet, SignalGetUntracked, SignalSet, SignalUpdate, SignalWith, - SuspenseContext, WriteSignal, + SpecialNonReactiveZone, SuspenseContext, WriteSignal, }; use std::{ any::Any, @@ -523,7 +523,13 @@ where pub fn refetch(&self) { _ = with_runtime(|runtime| { runtime.resource(self.id, |resource: &ResourceState| { - resource.refetch() + #[cfg(debug_assertions)] + let prev = SpecialNonReactiveZone::enter(); + resource.refetch(); + #[cfg(debug_assertions)] + { + SpecialNonReactiveZone::exit(prev); + } }) }); } diff --git a/leptos_reactive/src/runtime.rs b/leptos_reactive/src/runtime.rs index 8a36b4ed34..17c277743b 100644 --- a/leptos_reactive/src/runtime.rs +++ b/leptos_reactive/src/runtime.rs @@ -776,9 +776,12 @@ impl RuntimeId { with_runtime(|runtime| { let untracked_result; - if !diagnostics { - SpecialNonReactiveZone::enter(); - } + #[cfg(debug_assertions)] + let prev = if !diagnostics { + SpecialNonReactiveZone::enter() + } else { + false + }; let prev_observer = SetObserverOnDrop(self, runtime.observer.take()); @@ -788,8 +791,9 @@ impl RuntimeId { runtime.observer.set(prev_observer.1); std::mem::forget(prev_observer); // avoid Drop + #[cfg(debug_assertions)] if !diagnostics { - SpecialNonReactiveZone::exit(); + SpecialNonReactiveZone::exit(prev); } untracked_result @@ -1235,9 +1239,13 @@ impl Drop for SetBatchingOnDrop { pub fn on_cleanup(cleanup_fn: impl FnOnce() + 'static) { #[cfg(debug_assertions)] let cleanup_fn = move || { - crate::SpecialNonReactiveZone::enter(); + #[cfg(debug_assertions)] + let prev = crate::SpecialNonReactiveZone::enter(); cleanup_fn(); - crate::SpecialNonReactiveZone::exit(); + #[cfg(debug_assertions)] + { + crate::SpecialNonReactiveZone::exit(prev); + } }; push_cleanup(Box::new(cleanup_fn)) }