Giter Club home page Giter Club logo

Comments (5)

ealmloff avatar ealmloff commented on June 10, 2024

Here is a smaller reproduction of the issue:

#![allow(non_snake_case)]

use dioxus::prelude::*;

fn main() {
    launch(app)
}

fn app() -> Element {
    let mut count = use_signal(|| 0);

    let memorized = use_memo(move || count());

    let mut effect_run_count = use_hook(|| CopyValue::new(0));

    // This effect should only run once, because the memo only ever reads as 1
    use_effect(move || {
        // trigger a sync update on memo. This will mark the effect as dirty
        if *count.peek() == 0 {
            count += 1
        }

        println!("{memorized}");
        effect_run_count += 1;
        assert!(effect_run_count() <= 1, "This effect should only run once");
    });


    rsx! { "hello world" }
}

Memo<T> is recomputing the value when you read the memo inside the effect. Recomputing the value sets the signal with the memorized value which causes all reactive closures that read the memo to rerun, including the effect that it recomputed the value inside.

We can fix this issue by unsubscribing to all signals before rerunning reactive scopes (use_memo, use_effect, use_resource, components). With that fix, the effect should not be subscribed to the memo until the effect is finished running which avoids the duplicate run

from dioxus.

pablosichert avatar pablosichert commented on June 10, 2024

Memo<T> is recomputing the value when you read the memo inside the effect.

Why is recomputing the value necessary at that point?

from dioxus.

ealmloff avatar ealmloff commented on June 10, 2024

Memo<T> is recomputing the value when you read the memo inside the effect.

Why is recomputing the value necessary at that point?

Memos use a push/pull based system to try to maintain consistent. They don't recompute until one of these two conditions is met:

  1. The value is read
  2. All other rendering is done
let mut count = use_signal(|| 0);
// memorized will run once to get it's initial value
let memorized = use_memo(move || count() * 2);
// memorized is marked as dirty, but doesn't rerun - Lazily reruning lets us skip calculating the memo value when count = 1
count += 1;
// memorized is marked as dirty, but doesn't rerun
count += 1;

// memorized is read so it is rerun - this means we can never observe an out of date value
println!("{memorized}");

// memorized is marked as dirty, but doesn't rerun
count += 1;

// Once the component (and any parent components are done rendering, memorized is recomputed to see if any reactive closures/components need to be rerun)

from dioxus.

pablosichert avatar pablosichert commented on June 10, 2024

Interesting, thank you for elaborating.

What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?

from dioxus.

ealmloff avatar ealmloff commented on June 10, 2024

Interesting, thank you for elaborating.

What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?

Yes, exactly. It will rerun at most once for every write to a signal that is subscribes to

from dioxus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.