Skip to content

Commit

Permalink
fix: suppress warning about non-reactivity when calling .refetch()
Browse files Browse the repository at this point in the history
…(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
  • Loading branch information
gbj authored Aug 24, 2023
1 parent 65d4e98 commit ec4bd76
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
8 changes: 4 additions & 4 deletions leptos_dom/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ pub fn add_event_listener<E>(
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);
});
}
}
Expand All @@ -88,10 +88,10 @@ pub(crate) fn add_event_listener_undelegated<E>(
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);
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions leptos_dom/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
}
Expand Down Expand Up @@ -273,10 +273,10 @@ pub fn debounce<T: 'static>(
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);
};
}
}
Expand Down Expand Up @@ -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);
};
}
}
Expand Down Expand Up @@ -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);
};
}
}
Expand Down
20 changes: 10 additions & 10 deletions leptos_reactive/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
10 changes: 8 additions & 2 deletions leptos_reactive/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -523,7 +523,13 @@ where
pub fn refetch(&self) {
_ = with_runtime(|runtime| {
runtime.resource(self.id, |resource: &ResourceState<S, T>| {
resource.refetch()
#[cfg(debug_assertions)]
let prev = SpecialNonReactiveZone::enter();
resource.refetch();
#[cfg(debug_assertions)]
{
SpecialNonReactiveZone::exit(prev);
}
})
});
}
Expand Down
20 changes: 14 additions & 6 deletions leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit ec4bd76

Please sign in to comment.