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

Dynamic state: add_computed_var, add_event_handler #1139

Closed
wants to merge 7 commits into from

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Jun 5, 2023

Deterministically add vars to the state tree at compile time, ensuring that new vars are reactive throughout all substates and accounted for in internal bookkeeping dicts (_propagate_var).

Add event handlers dynamically.

Allow "local" substates, defined within a function, for more complex generic components that extend state at compile time.

Track a copy of inherited_vars per state class, not directly referencing the parent state vars.

Fix #893

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

After these steps, you're ready to open a pull request.

a. Give a descriptive title to your PR.

b. Describe your changes.

c. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if such).

Making (compile-time) dynamic state easier to work with, using decorators to
add computed vars and event handlers at runtime.

* get_substates is no longer lru_cache: dynamically added substates should also be considered
* _propagate_var: assigns var to key in cls.vars and cls.computed_vars (if applicable) and recursively
  assigns the var to a key in cls.inherited_vars in all substates.
* Fix EventHandler function names derived from state classes that are not defined at module scope
Call _set_var on a separate state class to avoid partially adding a new var to
the main TestState class
The main State class now uses a cached_var for dynamic route variables, and a
cached_var is used in the test state, so side effect tracking can be re-enabled
now.
add_var and add_computed_var are tested with nested substates
keep track of inherited vars from the parent in addition to any vars defined in
the parent state itself.
must call get_parent_state to get the state class object
does not seem like this should work with the current implementation, but
indeed, it seems to.

i actually tried to write the code in state.py _propagate_var to put backend
vars in the `backend_vars` dict, but that actually didn't work...

so leaving the tests here, since they do pass, and hopefully further
refactoring might dislodge something in this area that can be improved.
@masenf
Copy link
Collaborator Author

masenf commented Nov 30, 2023

Closing this.

  1. Lots of code drift since June, would be hard to update this PR.
  2. With the release of reflex-0.3.5, there is less need to augment existing State classes: can now just create new subclasses of rx.State (REF-1052/rx.State as Base State #2146), either statically or dynamically.
  3. With an eye toward Pydantic V2 (Upgrade to Pydantic 2.0 #1539), reflex needs to stop allowing runtime modification of Base-derived objects, like State

@masenf masenf closed this Nov 30, 2023
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.

Grandchild states do not correctly see dynamic vars added to parent state
1 participant