Skip to content

Commit

Permalink
fix: improved diagnostics about non-reactive signal access (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Jul 3, 2023
1 parent 66f54e7 commit f69c28d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions leptos_dom/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ where
let mut repr = ComponentRepr::new_with_id(name, id);

// disposed automatically when the parent scope is disposed
let (child, _) = cx
.run_child_scope(|cx| cx.untrack(|| children_fn(cx).into_view(cx)));
let (child, _) = cx.run_child_scope(|cx| {
cx.untrack_with_diagnostics(|| children_fn(cx).into_view(cx))
});

repr.children.push(child);

Expand Down
16 changes: 12 additions & 4 deletions leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,17 @@ impl RuntimeId {
instrument(level = "trace", skip_all,)
)]
#[inline(always)]
pub(crate) fn untrack<T>(self, f: impl FnOnce() -> T) -> T {
pub(crate) fn untrack<T>(
self,
f: impl FnOnce() -> T,
diagnostics: bool,
) -> T {
with_runtime(self, |runtime| {
let untracked_result;

SpecialNonReactiveZone::enter();
if !diagnostics {
SpecialNonReactiveZone::enter();
}

let prev_observer =
SetObserverOnDrop(self, runtime.observer.take());
Expand All @@ -493,7 +499,9 @@ impl RuntimeId {
runtime.observer.set(prev_observer.1);
std::mem::forget(prev_observer); // avoid Drop

SpecialNonReactiveZone::exit();
if !diagnostics {
SpecialNonReactiveZone::exit();
}

untracked_result
})
Expand Down Expand Up @@ -758,7 +766,7 @@ impl RuntimeId {
cur_deps_value.replace(Some(deps_value.clone()));

let callback_value =
Some(self.untrack(wrapped_callback.clone()));
Some(self.untrack(wrapped_callback.clone(), false));

prev_callback_value.replace(callback_value);

Expand Down
9 changes: 8 additions & 1 deletion leptos_reactive/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ impl Scope {
)]
#[inline(always)]
pub fn untrack<T>(&self, f: impl FnOnce() -> T) -> T {
self.runtime.untrack(f)
self.runtime.untrack(f, false)
}

#[doc(hidden)]
/// Suspends reactive tracking but keeps the diagnostic warnings for
/// untracked functions.
pub fn untrack_with_diagnostics<T>(&self, f: impl FnOnce() -> T) -> T {
self.runtime.untrack(f, true)
}
}

Expand Down
3 changes: 3 additions & 0 deletions leptos_reactive/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use crate::{
/// // setting name only causes name to log, not count
/// set_name.set("Bob".into());
/// ```
#[track_caller]
pub fn create_slice<T, O, S>(
cx: Scope,
signal: RwSignal<T>,
Expand All @@ -85,6 +86,7 @@ where

/// Takes a memoized, read-only slice of a signal. This is equivalent to the
/// read-only half of [`create_slice`].
#[track_caller]
pub fn create_read_slice<T, O>(
cx: Scope,
signal: RwSignal<T>,
Expand All @@ -98,6 +100,7 @@ where

/// Creates a setter to access one slice of a signal. This is equivalent to the
/// write-only half of [`create_slice`].
#[track_caller]
pub fn create_write_slice<T, O>(
cx: Scope,
signal: RwSignal<T>,
Expand Down

0 comments on commit f69c28d

Please sign in to comment.