|
@@ -88,6 +88,9 @@ class App(Base):
|
|
# The component to render if there is a connection error to the server.
|
|
# The component to render if there is a connection error to the server.
|
|
connect_error_component: Optional[Component] = None
|
|
connect_error_component: Optional[Component] = None
|
|
|
|
|
|
|
|
+ # The async server name space
|
|
|
|
+ event_namespace: Optional[AsyncNamespace] = None
|
|
|
|
+
|
|
def __init__(self, *args, **kwargs):
|
|
def __init__(self, *args, **kwargs):
|
|
"""Initialize the app.
|
|
"""Initialize the app.
|
|
|
|
|
|
@@ -127,11 +130,10 @@ class App(Base):
|
|
self.socket_app = ASGIApp(self.sio, socketio_path="")
|
|
self.socket_app = ASGIApp(self.sio, socketio_path="")
|
|
|
|
|
|
# Create the event namespace and attach the main app. Not related to any paths.
|
|
# Create the event namespace and attach the main app. Not related to any paths.
|
|
- event_namespace = EventNamespace("/event", self)
|
|
|
|
|
|
+ self.event_namespace = EventNamespace("/event", self)
|
|
|
|
|
|
# Register the event namespace with the socket.
|
|
# Register the event namespace with the socket.
|
|
- self.sio.register_namespace(event_namespace)
|
|
|
|
-
|
|
|
|
|
|
+ self.sio.register_namespace(self.event_namespace)
|
|
# Mount the socket app with the API.
|
|
# Mount the socket app with the API.
|
|
self.api.mount(str(constants.Endpoint.EVENT), self.socket_app)
|
|
self.api.mount(str(constants.Endpoint.EVENT), self.socket_app)
|
|
|
|
|
|
@@ -592,9 +594,6 @@ def upload(app: App):
|
|
Args:
|
|
Args:
|
|
files: The file(s) to upload.
|
|
files: The file(s) to upload.
|
|
|
|
|
|
- Returns:
|
|
|
|
- The state update after processing the event.
|
|
|
|
-
|
|
|
|
Raises:
|
|
Raises:
|
|
ValueError: if there are no args with supported annotation.
|
|
ValueError: if there are no args with supported annotation.
|
|
"""
|
|
"""
|
|
@@ -603,10 +602,10 @@ def upload(app: App):
|
|
for file in files:
|
|
for file in files:
|
|
assert file.filename is not None
|
|
assert file.filename is not None
|
|
file.filename = file.filename.split(":")[-1]
|
|
file.filename = file.filename.split(":")[-1]
|
|
-
|
|
|
|
# Get the state for the session.
|
|
# Get the state for the session.
|
|
state = app.state_manager.get_state(token)
|
|
state = app.state_manager.get_state(token)
|
|
-
|
|
|
|
|
|
+ # get the current session ID
|
|
|
|
+ sid = state.get_sid()
|
|
# get the current state(parent state/substate)
|
|
# get the current state(parent state/substate)
|
|
path = handler.split(".")[:-1]
|
|
path = handler.split(".")[:-1]
|
|
current_state = state.get_substate(path)
|
|
current_state = state.get_substate(path)
|
|
@@ -636,12 +635,17 @@ def upload(app: App):
|
|
name=handler,
|
|
name=handler,
|
|
payload={handler_upload_param[0]: files},
|
|
payload={handler_upload_param[0]: files},
|
|
)
|
|
)
|
|
- # TODO: refactor this to handle yields.
|
|
|
|
- update = await state._process(event).__anext__()
|
|
|
|
-
|
|
|
|
|
|
+ async for update in state._process(event):
|
|
|
|
+ # Postprocess the event.
|
|
|
|
+ update = await app.postprocess(state, event, update)
|
|
|
|
+ # Send update to client
|
|
|
|
+ await asyncio.create_task(
|
|
|
|
+ app.event_namespace.emit( # type: ignore
|
|
|
|
+ str(constants.SocketEvent.EVENT), update.json(), to=sid
|
|
|
|
+ )
|
|
|
|
+ )
|
|
# Set the state for the session.
|
|
# Set the state for the session.
|
|
app.state_manager.set_state(event.token, state)
|
|
app.state_manager.set_state(event.token, state)
|
|
- return update
|
|
|
|
|
|
|
|
return upload_file
|
|
return upload_file
|
|
|
|
|