(
websocket: WebSocket,
path: str,
component: ComponentType,
send: Callable[[Any], None],
recv: Callable[[], Any | None],
)
| 203 | |
| 204 | |
| 205 | def _dispatch_in_thread( |
| 206 | websocket: WebSocket, |
| 207 | path: str, |
| 208 | component: ComponentType, |
| 209 | send: Callable[[Any], None], |
| 210 | recv: Callable[[], Any | None], |
| 211 | ) -> NoReturn: |
| 212 | dispatch_thread_info_created = ThreadEvent() |
| 213 | dispatch_thread_info_ref: reactpy.Ref[_DispatcherThreadInfo | None] = reactpy.Ref( |
| 214 | None |
| 215 | ) |
| 216 | |
| 217 | @copy_current_request_context |
| 218 | def run_dispatcher() -> None: |
| 219 | loop = asyncio.new_event_loop() |
| 220 | asyncio.set_event_loop(loop) |
| 221 | |
| 222 | thread_send_queue: ThreadQueue[Any] = ThreadQueue() |
| 223 | async_recv_queue: AsyncQueue[Any] = AsyncQueue() |
| 224 | |
| 225 | async def send_coro(value: Any) -> None: |
| 226 | thread_send_queue.put(value) |
| 227 | |
| 228 | async def main() -> None: |
| 229 | search = request.query_string.decode() |
| 230 | await serve_layout( |
| 231 | reactpy.Layout( |
| 232 | ConnectionContext( |
| 233 | component, |
| 234 | value=Connection( |
| 235 | scope=request.environ, |
| 236 | location=Location( |
| 237 | pathname=f"/{path}", |
| 238 | search=f"?{search}" if search else "", |
| 239 | ), |
| 240 | carrier=_FlaskCarrier(request, websocket), |
| 241 | ), |
| 242 | ), |
| 243 | ), |
| 244 | send_coro, |
| 245 | async_recv_queue.get, |
| 246 | ) |
| 247 | |
| 248 | main_future = asyncio.ensure_future(main(), loop=loop) |
| 249 | |
| 250 | dispatch_thread_info_ref.current = _DispatcherThreadInfo( |
| 251 | dispatch_loop=loop, |
| 252 | dispatch_future=main_future, |
| 253 | thread_send_queue=thread_send_queue, |
| 254 | async_recv_queue=async_recv_queue, |
| 255 | ) |
| 256 | dispatch_thread_info_created.set() |
| 257 | |
| 258 | loop.run_until_complete(main_future) |
| 259 | |
| 260 | Thread(target=run_dispatcher, daemon=True).start() |
| 261 | |
| 262 | dispatch_thread_info_created.wait() |
no test coverage detected