File tree Expand file tree Collapse file tree 3 files changed +17
-4
lines changed
Expand file tree Collapse file tree 3 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -2463,16 +2463,20 @@ class OnLoadInternalState(State):
24632463 This is a separate substate to avoid deserializing the entire state tree for every page navigation.
24642464 """
24652465
2466+ # Cannot properly annotate this as `App` due to circular import issues.
2467+ _app_ref : ClassVar = None
2468+
24662469 def on_load_internal (self ) -> list [Event | EventSpec | event .EventCallback ] | None :
24672470 """Queue on_load handlers for the current page.
24682471
24692472 Returns:
24702473 The list of events to queue for on load handling.
24712474 """
2472- # Do not app._compile()! It should be already compiled by now.
2473- load_events = prerequisites .get_and_validate_app ().app .get_load_events (
2474- self .router ._page .path
2475- )
2475+ app = type (self )._app_ref or prerequisites .get_and_validate_app ().app
2476+ # Cache the app reference for subsequent calls.
2477+ if type (self )._app_ref is None :
2478+ type(self )._app_ref = app
2479+ load_events = app .get_load_events (self .router ._page .path )
24762480 if not load_events :
24772481 self .is_hydrated = True
24782482 return None # Fast path for navigation with no on_load events defined.
@@ -2646,6 +2650,9 @@ def reload_state_module(
26462650 state: Recursive argument for the state class to reload.
26472651
26482652 """
2653+ # Reset the _app_ref of OnLoadInternalState to avoid stale references.
2654+ if state is OnLoadInternalState :
2655+ state ._app_ref = None
26492656 # Clean out all potentially dirty states of reloaded modules.
26502657 for pd_state in tuple (state ._potentially_dirty_states ):
26512658 with contextlib .suppress (ValueError ):
Original file line number Diff line number Diff line change 77from collections .abc import Generator
88from contextlib import nullcontext as does_not_raise
99from pathlib import Path
10+ from typing import ClassVar
1011from unittest .mock import AsyncMock
1112
1213import pytest
@@ -945,6 +946,7 @@ class DynamicState(BaseState):
945946 is_hydrated : bool = False
946947 loaded : int = 0
947948 counter : int = 0
949+ _app_ref : ClassVar = None
948950
949951 @rx .event
950952 def on_load (self ):
@@ -982,6 +984,7 @@ def test_dynamic_arg_shadow(
982984 app_module_mock: Mocked app module.
983985 mocker: pytest mocker object.
984986 """
987+ DynamicState ._app_ref = None
985988 arg_name = "counter"
986989 route = f"/test/[{ arg_name } ]"
987990 app = app_module_mock .app = App (_state = DynamicState )
@@ -1030,6 +1033,7 @@ async def test_dynamic_route_var_route_change_completed_on_load(
10301033 app_module_mock: Mocked app module.
10311034 mocker: pytest mocker object.
10321035 """
1036+ DynamicState ._app_ref = None
10331037 arg_name = "dynamic"
10341038 route = f"test/[{ arg_name } ]"
10351039 app = app_module_mock .app = App (_state = DynamicState )
Original file line number Diff line number Diff line change @@ -2948,6 +2948,7 @@ async def test_preprocess(
29482948 expected: Expected delta.
29492949 mocker: pytest mock object.
29502950 """
2951+ OnLoadInternalState ._app_ref = None
29512952 mocker .patch (
29522953 "reflex.state.State.class_subclasses" , {test_state , OnLoadInternalState }
29532954 )
@@ -3002,6 +3003,7 @@ async def test_preprocess_multiple_load_events(
30023003 token: A token.
30033004 mocker: pytest mock object.
30043005 """
3006+ OnLoadInternalState ._app_ref = None
30053007 mocker .patch (
30063008 "reflex.state.State.class_subclasses" , {OnLoadState , OnLoadInternalState }
30073009 )
You can’t perform that action at this time.
0 commit comments