finalize api for plugin events in backend

just need frontend impl now
This commit is contained in:
AAGaming
2023-12-31 20:29:19 -05:00
parent db96121304
commit c5ea95a787
8 changed files with 93 additions and 50 deletions
+10 -10
View File
@@ -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 })