Skip to content

Commit 7046785

Browse files
use get_event_handler in upload, cherry-pick from #6100 (#6130)
1 parent 8e3fc08 commit 7046785

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

reflex/app.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@
7777
from reflex.event import (
7878
_EVENT_FIELDS,
7979
Event,
80-
EventHandler,
8180
EventSpec,
8281
EventType,
8382
IndividualEventType,
8483
get_hydrate_event,
8584
noop,
8685
)
86+
from reflex.istate.proxy import StateProxy
8787
from reflex.page import DECORATED_PAGES
8888
from reflex.route import (
8989
get_route_args,
@@ -1619,6 +1619,8 @@ def _process_background(
16191619
if not handler.is_background:
16201620
return None
16211621

1622+
substate = StateProxy(substate)
1623+
16221624
async def _coro():
16231625
"""Coroutine to process the event and emit updates inside an asyncio.Task.
16241626
@@ -1934,21 +1936,14 @@ async def upload_file(request: Request):
19341936
substate_token = _substate_key(token, handler.rpartition(".")[0])
19351937
state = await app.state_manager.get_state(substate_token)
19361938

1937-
# get the current session ID
1938-
# get the current state(parent state/substate)
1939-
path = handler.split(".")[:-1]
1940-
current_state = state.get_substate(path)
19411939
handler_upload_param = ()
19421940

1943-
# get handler function
1944-
func = getattr(type(current_state), handler.split(".")[-1])
1941+
_current_state, event_handler = state._get_event_handler(handler)
19451942

1946-
# check if there exists any handler args with annotation, list[UploadFile]
1947-
if isinstance(func, EventHandler):
1948-
if func.is_background:
1949-
msg = f"@rx.event(background=True) is not supported for upload handler `{handler}`."
1950-
raise UploadTypeError(msg)
1951-
func = func.fn
1943+
if event_handler.is_background:
1944+
msg = f"@rx.event(background=True) is not supported for upload handler `{handler}`."
1945+
raise UploadTypeError(msg)
1946+
func = event_handler.fn
19521947
if isinstance(func, functools.partial):
19531948
func = func.func
19541949
for k, v in get_type_hints(func).items():

reflex/state.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,13 +1709,11 @@ async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE:
17091709
)
17101710
return getattr(other_state, var_data.field_name)
17111711

1712-
def _get_event_handler(
1713-
self, event: Event
1714-
) -> tuple[BaseState | StateProxy, EventHandler]:
1712+
def _get_event_handler(self, event: Event | str) -> tuple[BaseState, EventHandler]:
17151713
"""Get the event handler for the given event.
17161714
17171715
Args:
1718-
event: The event to get the handler for.
1716+
event: The event to get the handler for, or a dotted handler name string.
17191717
17201718
17211719
Returns:
@@ -1725,18 +1723,15 @@ def _get_event_handler(
17251723
ValueError: If the event handler or substate is not found.
17261724
"""
17271725
# Get the event handler.
1728-
path = event.name.split(".")
1726+
name = event.name if isinstance(event, Event) else event
1727+
path = name.split(".")
17291728
path, name = path[:-1], path[-1]
17301729
substate = self.get_substate(path)
17311730
if not substate:
17321731
msg = "The value of state cannot be None when processing an event."
17331732
raise ValueError(msg)
17341733
handler = substate.event_handlers[name]
17351734

1736-
# For background tasks, proxy the state
1737-
if handler.is_background:
1738-
substate = StateProxy(substate)
1739-
17401735
return substate, handler
17411736

17421737
async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
@@ -1751,6 +1746,10 @@ async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
17511746
# Get the event handler.
17521747
substate, handler = self._get_event_handler(event)
17531748

1749+
# For background tasks, proxy the state.
1750+
if handler.is_background:
1751+
substate = StateProxy(substate)
1752+
17541753
# Run the event generator and yield state updates.
17551754
async for update in self._process_event(
17561755
handler=handler,

0 commit comments

Comments
 (0)