Skip to content

Commit

Permalink
fix: only mark a memo Check if it isn't already Dirty (closes #3339)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Dec 12, 2024
1 parent f7751fb commit cbddda0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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
37 changes: 37 additions & 0 deletions reactive_graph/tests/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,40 @@ 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)]

// 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 cbddda0

Please sign in to comment.