Skip to content

Commit

Permalink
fix: only mark memos Check if they aren't already Dirty (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Dec 16, 2024
1 parent 753cfe8 commit a6aa111
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion reactive_graph/src/computed/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ where
fn mark_check(&self) {
{
let mut lock = self.write().or_poisoned();
lock.state = ReactiveNodeState::Check;
if lock.state != ReactiveNodeState::Dirty {
lock.state = ReactiveNodeState::Check;
}
}
for sub in (&self.read().or_poisoned().subscribers).into_iter() {
sub.mark_check();
Expand Down
2 changes: 1 addition & 1 deletion reactive_graph/src/graph/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a> IntoIterator for &'a SourceSet {
self.0.iter()
}
}
#[derive(Default, Clone)]
#[derive(Debug, Default, Clone)]
pub struct SubscriberSet(Vec<AnySubscriber>);

impl SubscriberSet {
Expand Down
40 changes: 40 additions & 0 deletions reactive_graph/tests/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,43 @@ fn unsync_derived_signal_and_memo() {
assert_eq!(f.with(|n| *n), 6);
assert_eq!(f.get_untracked(), 6);
}

#[test]
fn memo_updates_even_if_not_read_until_later() {
#![allow(clippy::bool_assert_comparison)]

let owner = Owner::new();
owner.set();

// regression test for https://github.com/leptos-rs/leptos/issues/3339

let input = RwSignal::new(0);
let first_memo = Memo::new(move |_| input.get() == 1);
let second_memo = Memo::new(move |_| first_memo.get());

assert_eq!(input.get(), 0);
assert_eq!(first_memo.get(), false);

println!("update to 1");
input.set(1);
assert_eq!(input.get(), 1);
println!("read memo 1");
assert_eq!(first_memo.get(), true);
println!("read memo 2");
assert_eq!(second_memo.get(), true);

// this time, we don't read the memo
println!("\nupdate to 2");
input.set(2);
assert_eq!(input.get(), 2);
println!("read memo 1");
assert_eq!(first_memo.get(), false);

println!("\nupdate to 3");
input.set(3);
assert_eq!(input.get(), 3);
println!("read memo 1");
assert_eq!(first_memo.get(), false);
println!("read memo 2");
assert_eq!(second_memo.get(), false);
}

0 comments on commit a6aa111

Please sign in to comment.