Build session-start callback that executes the user `main` handler. Args: main: User-provided app entry handler. Returns: Async callback that initializes page context and runs `main`.
(
main: Optional[AppCallable],
)
| 314 | |
| 315 | |
| 316 | def __get_on_session_created( |
| 317 | main: Optional[AppCallable], |
| 318 | ) -> Callable[[Session], Awaitable[None]]: |
| 319 | """ |
| 320 | Build session-start callback that executes the user `main` handler. |
| 321 | |
| 322 | Args: |
| 323 | main: User-provided app entry handler. |
| 324 | |
| 325 | Returns: |
| 326 | Async callback that initializes page context and runs `main`. |
| 327 | """ |
| 328 | |
| 329 | async def on_session_created(session: Session): |
| 330 | """ |
| 331 | Initialize per-session context and execute app entry handler. |
| 332 | |
| 333 | Args: |
| 334 | session: Active page session. |
| 335 | """ |
| 336 | |
| 337 | logger.info("App session started") |
| 338 | try: |
| 339 | assert main is not None |
| 340 | _context_page.set(session.page) |
| 341 | context.reset_auto_update() |
| 342 | if inspect.iscoroutinefunction(main): |
| 343 | await main(session.page) |
| 344 | |
| 345 | elif inspect.isasyncgenfunction(main): |
| 346 | async for _ in main(session.page): |
| 347 | await session.after_event(session.page) |
| 348 | |
| 349 | elif inspect.isgeneratorfunction(main): |
| 350 | for _ in main(session.page): |
| 351 | await session.after_event(session.page) |
| 352 | else: |
| 353 | # run synchronously |
| 354 | main(session.page) |
| 355 | |
| 356 | await session.after_event(session.page) |
| 357 | |
| 358 | except Exception as e: |
| 359 | logger.error("Unhandled error in main() handler", exc_info=True) |
| 360 | session.error(f"{e}\n{traceback.format_exc()}") |
| 361 | |
| 362 | return on_session_created |
| 363 | |
| 364 | |
| 365 | async def __run_socket_server( |
no outgoing calls
no test coverage detected