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

Independent States #3206

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,18 +1464,18 @@ def _as_state_update(
Returns:
The valid StateUpdate containing the events and final flag.
"""
# get the delta from the root of the state tree
# Get the delta after processing the event.
delta = self.get_delta(include_substates=True)
state = self
while state.parent_state is not None:
state = state.parent_state
delta.update(state.get_delta(include_substates=False))

token = self.router.session.client_token

# Convert valid EventHandler and EventSpec into Event
fixed_events = fix_events(self._check_valid(handler, events), token)

# Get the delta after processing the event.
delta = state.get_delta()
state._clean()

return StateUpdate(
Expand Down Expand Up @@ -1589,9 +1589,12 @@ def _potentially_dirty_substates(cls) -> set[Type[BaseState]]:
)
return fetch_substates

def get_delta(self) -> Delta:
def get_delta(self, include_substates: bool = True) -> Delta:
"""Get the delta for the state.

Args:
include_substates: Whether to include substates in the delta.

Returns:
The delta for the state.
"""
Expand All @@ -1618,9 +1621,12 @@ def get_delta(self) -> Delta:
delta[self.get_full_name()] = subdelta

# Recursively find the substate deltas.
substates = self.substates
for substate in self.dirty_substates.union(self._always_dirty_substates):
delta.update(substates[substate].get_delta())
if include_substates:
substates = self.substates
for substate_name in self.dirty_substates.union(
self._always_dirty_substates
):
delta.update(substates[substate_name].get_delta())

# Format the delta.
delta = format.format_state(delta)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,6 @@ async def test_process_event_substate(test_state, child_state, grandchild_state)
assert update.delta == {
"test_state": {"sum": 3.14, "upper": ""},
"test_state.child_state": {"value": "HI", "count": 24},
"test_state.child_state3.grandchild_state3": {"computed": ""},
}
test_state._clean()

Expand All @@ -757,7 +756,6 @@ async def test_process_event_substate(test_state, child_state, grandchild_state)
assert update.delta == {
"test_state": {"sum": 3.14, "upper": ""},
"test_state.child_state.grandchild_state": {"value2": "new"},
"test_state.child_state3.grandchild_state3": {"computed": ""},
}


Expand Down
Loading