Process an event. Args: app: The app to process the event for. event: The event to process. sid: The Socket.IO session id. headers: The client headers. client_ip: The client_ip. Yields: The state updates after processing the event.
(
app: App, event: Event, sid: str, headers: Dict, client_ip: str
)
| 878 | |
| 879 | |
| 880 | async def process( |
| 881 | app: App, event: Event, sid: str, headers: Dict, client_ip: str |
| 882 | ) -> AsyncIterator[StateUpdate]: |
| 883 | """Process an event. |
| 884 | |
| 885 | Args: |
| 886 | app: The app to process the event for. |
| 887 | event: The event to process. |
| 888 | sid: The Socket.IO session id. |
| 889 | headers: The client headers. |
| 890 | client_ip: The client_ip. |
| 891 | |
| 892 | Yields: |
| 893 | The state updates after processing the event. |
| 894 | """ |
| 895 | # Add request data to the state. |
| 896 | router_data = event.router_data |
| 897 | router_data.update( |
| 898 | { |
| 899 | constants.RouteVar.QUERY: format.format_query_params(event.router_data), |
| 900 | constants.RouteVar.CLIENT_TOKEN: event.token, |
| 901 | constants.RouteVar.SESSION_ID: sid, |
| 902 | constants.RouteVar.HEADERS: headers, |
| 903 | constants.RouteVar.CLIENT_IP: client_ip, |
| 904 | } |
| 905 | ) |
| 906 | # Get the state for the session exclusively. |
| 907 | async with app.state_manager.modify_state(event.token) as state: |
| 908 | # re-assign only when the value is different |
| 909 | if state.router_data != router_data: |
| 910 | # assignment will recurse into substates and force recalculation of |
| 911 | # dependent ComputedVar (dynamic route variables) |
| 912 | state.router_data = router_data |
| 913 | state.router = RouterData(router_data) |
| 914 | |
| 915 | # Preprocess the event. |
| 916 | update = await app.preprocess(state, event) |
| 917 | |
| 918 | # If there was an update, yield it. |
| 919 | if update is not None: |
| 920 | yield update |
| 921 | |
| 922 | # Only process the event if there is no update. |
| 923 | else: |
| 924 | if app._process_background(state, event) is not None: |
| 925 | # `final=True` allows the frontend send more events immediately. |
| 926 | yield StateUpdate(final=True) |
| 927 | return |
| 928 | |
| 929 | # Process the event synchronously. |
| 930 | async for update in state._process(event): |
| 931 | # Postprocess the event. |
| 932 | update = await app.postprocess(state, event, update) |
| 933 | |
| 934 | # Yield the update. |
| 935 | yield update |
| 936 | |
| 937 |