Given a `websocket`, and `handler_name`, attach the `handler_function` to the `WebSocket` instance, handling both synchronous and asynchronous handler functions. Creates a JavaScript proxy for the handler and wraps async handlers appropriately. Handles the `WebSocketEvent` wrap
(websocket, handler_name, handler_function)
| 45 | |
| 46 | |
| 47 | def _attach_event_handler(websocket, handler_name, handler_function): |
| 48 | """ |
| 49 | Given a `websocket`, and `handler_name`, attach the `handler_function` |
| 50 | to the `WebSocket` instance, handling both synchronous and asynchronous |
| 51 | handler functions. |
| 52 | |
| 53 | Creates a JavaScript proxy for the handler and wraps async handlers |
| 54 | appropriately. Handles the `WebSocketEvent` wrapping for all handlers. |
| 55 | """ |
| 56 | if is_awaitable(handler_function): |
| 57 | |
| 58 | async def async_wrapper(event): |
| 59 | await handler_function(WebSocketEvent(event)) |
| 60 | |
| 61 | wrapped_handler = create_proxy(async_wrapper) |
| 62 | else: |
| 63 | wrapped_handler = create_proxy( |
| 64 | lambda event: handler_function(WebSocketEvent(event)) |
| 65 | ) |
| 66 | # Note: Direct assignment (websocket[handler_name]) fails in Pyodide. |
| 67 | setattr(websocket, handler_name, wrapped_handler) |
| 68 | |
| 69 | |
| 70 | class WebSocketEvent: |
no test coverage detected