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

state: update inherited_vars and tracking dicts when adding vars #2822

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Mar 9, 2024

Cached vars were not updated when depending on dynamically added vars, because the dependency tracking dicts update substate_var_dependencies based on whether the var is inherited and inherited_vars was not being updated (and not being updated recursively) when vars were added to the state.

Now when a var is added, push the var down into all substates as an inherited var, and recursively (depth-first) update all substate _computed_var_dependencies and _substate_var_dependencies in case any of them were depending on a var that gets added later in the initialization process.

I don't think dynamically added vars get used that much outside of dynamic route args, so this probably doesn't come up in the wild that often.

Repro code

import reflex as rx

class State(rx.State):
    """The app state."""
    @rx.var
    def arg(self) -> int:
        return int(self.arg_str or 0)


class SubState(State):
    @rx.cached_var
    def cached_arg(self) -> str:
        return self.arg

    @rx.cached_var
    def cached_arg_str(self) -> str:
        return self.arg_str


def index() -> rx.Component:
    return rx.vstack(
        rx.heading(SubState.arg),
        rx.heading(SubState.cached_arg),
        rx.heading(SubState.cached_arg_str),
        rx.link("+", href=f"/{State.arg + 1}"),
        align="center",
        height="100vh",
    )


app = rx.App()
app.add_page(index)
app.add_page(index, route="/[arg_str]")

Navigate to http://localhost:3000/0 and notice all three numbers are zero. Clicking the plus should increment all three numbers, but without this fix, the cached_var that depends directly on the dynamic arg_str never gets updated because of the incorrect dependency tracking.

Cached vars were not updated when depending on dynamically added vars, because
the dependency tracking dicts update substate_var_dependencies based on whether
the var is inherited and inherited_vars was not being updated (and not being
updated recursively) when vars were added to the state.

Now when a var is added, push the var down into all substates as an inherited
var, and recursively (depth-first) update all substate
_computed_var_dependencies and _substate_var_dependencies in case any of them
were depending on a var that gets added later in the initialization process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant