diff options
Diffstat (limited to 'backend/decky_loader/wsrouter.py')
| -rw-r--r-- | backend/decky_loader/wsrouter.py | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/backend/decky_loader/wsrouter.py b/backend/decky_loader/wsrouter.py index 59cbe4a3..fa0707f7 100644 --- a/backend/decky_loader/wsrouter.py +++ b/backend/decky_loader/wsrouter.py @@ -1,3 +1,4 @@ +from _typeshed import DataclassInstance from logging import getLogger from asyncio import AbstractEventLoop, create_task @@ -8,7 +9,8 @@ from aiohttp.web import Application, WebSocketResponse, Request, Response, get from enum import IntEnum from typing import Callable, Coroutine, Dict, Any, cast, TypeVar, Type -from dataclasses import dataclass + +from dataclasses import asdict, is_dataclass from traceback import format_exc @@ -24,15 +26,9 @@ class MessageType(IntEnum): # WSMessage with slightly better typings class WSMessageExtra(WSMessage): + # TODO message typings here too data: Any type: WSMsgType -@dataclass -class Message: - data: Any - type: MessageType - -# @dataclass -# class CallMessage # see wsrouter.ts for typings @@ -133,7 +129,11 @@ class WSRouter: return ws # DataType defaults to None so that if a plugin opts in to strict pyright checking and attempts to pass data witbout specifying the type (or any), the type check fails - async def emit(self, event: str, data: DataType | None = None, data_type: Type[DataType]|None = None): + async def emit(self, event: str, data: DataType | None = None, data_type: Type[DataType] | None = None): self.logger.debug('Firing frontend event %s with args %s', data) + sent_data: Dict[Any, Any] | None = cast(Dict[Any, Any], data) + if is_dataclass(data): + data_as_dataclass = cast(DataclassInstance, data) + sent_data = asdict(data_as_dataclass) - await self.write({ "type": MessageType.EVENT.value, "event": event, "data": data }) + await self.write({ "type": MessageType.EVENT.value, "event": event, "data": sent_data }) |
