Skip to content

Commit

Permalink
Fix event handler returns (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
picklelo authored Apr 8, 2023
1 parent e8387c8 commit e96f1c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 23 additions & 20 deletions pynecone/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def compile(self, force_compile: bool = False):

async def process(
app: App, event: Event, sid: str, headers: Dict, client_ip: str
) -> Union[StateUpdate, List[StateUpdate]]:
) -> List[StateUpdate]:
"""Process an event.
Args:
Expand All @@ -415,45 +415,48 @@ async def process(
client_ip: The client_ip.
Returns:
The state update(s) after processing the event.
The state updates after processing the event.
"""
# Get the state for the session.
state = app.state_manager.get_state(event.token)

formatted_params = format.format_query_params(event.router_data)

# Pass router_data to the state of the App.
# Add request data to the state.
state.router_data = event.router_data
# also pass router_data to all substates
state.router_data.update(
{
constants.RouteVar.QUERY: format.format_query_params(event.router_data),
constants.RouteVar.CLIENT_TOKEN: event.token,
constants.RouteVar.SESSION_ID: sid,
constants.RouteVar.HEADERS: headers,
constants.RouteVar.CLIENT_IP: client_ip,
}
)

# Also pass router_data to all substates. (TODO: this isn't recursive currently)
for _, substate in state.substates.items():
substate.router_data = event.router_data
state.router_data[constants.RouteVar.QUERY] = formatted_params
state.router_data[constants.RouteVar.CLIENT_TOKEN] = event.token
state.router_data[constants.RouteVar.SESSION_ID] = sid
state.router_data[constants.RouteVar.HEADERS] = headers
state.router_data[constants.RouteVar.CLIENT_IP] = client_ip
substate.router_data = state.router_data

# Preprocess the event.
pre = await app.preprocess(state, event)
if pre is not None and not isinstance(pre, List):
return pre
if isinstance(pre, StateUpdate):
return [pre]
updates = pre

# Apply the event to the state.
updates = pre if pre else await state.process(event)
app.state_manager.set_state(event.token, state)

updates = updates if isinstance(updates, List) else [updates]
if updates is None:
updates = [await state.process(event)]
app.state_manager.set_state(event.token, state)

# Postprocess the event.
post_list = []
for update in updates:
post = await app.postprocess(state, event, update.delta) # type: ignore
post_list.append(post) if post else None

if post_list:
if len(post_list) > 0:
return [StateUpdate(delta=post) for post in post_list]

# Return the update.
# Return the updates.
return updates


Expand Down
2 changes: 2 additions & 0 deletions pynecone/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def fix_events(
# Fix the events created by the handler.
out = []
for e in events:
if not isinstance(e, (EventHandler, EventSpec)):
e = EventHandler(fn=e)
# Otherwise, create an event from the event spec.
if isinstance(e, EventHandler):
e = e()
Expand Down

0 comments on commit e96f1c4

Please sign in to comment.