MCPcopy Index your code
hub / github.com/pyscript/pyscript / _attach_event_handler

Function _attach_event_handler

core/src/stdlib/pyscript/websocket.py:47–67  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

45
46
47def _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
70class WebSocketEvent:

Callers 1

__setattr__Method · 0.85

Calls 3

is_awaitableFunction · 0.90
create_proxyFunction · 0.90
WebSocketEventClass · 0.85

Tested by

no test coverage detected