Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived Memo not triggering correctly when not rendered #3339

Closed
fdietze opened this issue Dec 9, 2024 · 2 comments
Closed

Derived Memo not triggering correctly when not rendered #3339

fdietze opened this issue Dec 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@fdietze
Copy link

fdietze commented Dec 9, 2024

Describe the bug
A derived Memo doesn't recompute correctly. Rendering it makes it trigger correctly.

Leptos Dependencies

leptos = { version = "0.7.0", features = ["csr", "nightly"] }

To Reproduce

#[component]
fn App() -> impl IntoView {
    let (input, set_input) = signal(0);
    let first_memo = Memo::new(move |_| input() == 1);
    let second_memo = Memo::new(move |_| first_memo());

    let (submitted_value, set_submitted_value) = signal(false);

    let submit = move || {
        set_submitted_value(second_memo());
    };

    view! {
        Press these buttons in order to reproduce the bug:
        <div>
            <button on:click=move |_| { set_input(1) }>1</button>
            <button on:click=move |_| { submit() }>submit</button>
            <button on:click=move |_| { set_input(2) }>2</button>
            <button on:click=move |_| { set_input(3) }>3</button>
            <button on:click=move |_| { submit() }>submit</button>
        </div>
        Input:
        {move || input()}
        <br />
        First Memo:
        {move || first_memo()}
        <br />
        <Show when=|| {
            false
        }>
            Second Memo: {move || second_memo()}<br />
            Enabling this Show, makes the second memo trigger correctly.
        </Show>
        <br />
        Submitted:
        {move || submitted_value()}
    }
}
  1. Press the buttons in order.
  2. BUG: the submitted value is true, but should be false
  3. Change the false in <Show> to true, to render second_memo
  4. Press the buttons in order.
  5. The submitted value is correct: false

Expected behavior
Derived memos should always have the latest state available when their inputs change.

@gbj
Copy link
Collaborator

gbj commented Dec 9, 2024

Thanks!

As a note to myself: the problem occurs specifically when the first memo is triggered twice before the second memo is ever read again, so that by the time .update_if_necessary() runs on the second memo during its read, its parent has already marked itself clean again. The second memo should probably have been marked dirty when the first one changed.

@gbj
Copy link
Collaborator

gbj commented Dec 12, 2024

The issue here turns out to be super simple and a regression in my rewrite of the reactive graph for 0.7: the Memo::mark_check function should only mark a Memo as being in the Check state if it isn't already Dirty, which is a stronger claim. The one-line change in the PR (plus regression test) close this issue.

@gbj gbj closed this as completed in a6aa111 Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants