mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-06-17 08:47:49 +00:00
finalize api for plugin events in backend
just need frontend impl now
This commit is contained in:
@@ -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 })
|
||||
|
||||
Reference in New Issue
Block a user