@@ -159,16 +159,20 @@ async def __call__(
159159 # Start a loop that handles ASGI websocket events
160160 async with ReactPyWebsocket (scope , receive , send , parent = self .parent ) as ws : # type: ignore
161161 while True :
162+ # Wait for the webserver to notify us of a new event
162163 event : dict [str , Any ] = await ws .receive (raw = True ) # type: ignore
163- if event ["type" ] == "websocket.receive" and event ["text" ]:
164+
165+ # If the event is a `receive` event, parse the message and send it to the rendering queue
166+ if event ["type" ] == "websocket.receive" :
164167 msg : dict [str , str ] = orjson .loads (event ["text" ])
165168 if msg .get ("type" ) == "layout-event" :
166- queue_put_func = ws .recv_queue .put (msg )
167- await queue_put_func
168- else :
169+ await ws .rendering_queue .put (msg )
170+ else : # pragma: no cover
169171 await asyncio .to_thread (
170172 _logger .warning , f"Unknown message type: { msg .get ('type' )} "
171173 )
174+
175+ # If the event is a `disconnect` event, break the rendering loop and close the connection
172176 elif event ["type" ] == "websocket.disconnect" :
173177 break
174178
@@ -184,7 +188,7 @@ def __init__(
184188 super ().__init__ (scope = scope , receive = receive , send = send ) # type: ignore
185189 self .scope = scope
186190 self .parent = parent
187- self .recv_queue : asyncio .Queue [dict [str , str ]] = asyncio .Queue ()
191+ self .rendering_queue : asyncio .Queue [dict [str , str ]] = asyncio .Queue ()
188192 self .dispatcher : asyncio .Task [Any ] | None = None
189193
190194 async def __aenter__ (self ) -> ReactPyWebsocket :
@@ -232,7 +236,7 @@ async def run_dispatcher(self) -> None:
232236 await serve_layout (
233237 Layout (ConnectionContext (component (), value = connection )),
234238 self .send_json ,
235- self .recv_queue .get , # type: ignore
239+ self .rendering_queue .get , # type: ignore
236240 )
237241
238242 # Manually log exceptions since this function is running in a separate asyncio task.
0 commit comments