Better tracking of client event handlers

This commit is contained in:
Andrew Ferrazzutti 2022-04-09 03:04:00 -04:00
parent aee66976f6
commit 075bf3e60f
2 changed files with 15 additions and 12 deletions

View File

@ -22,7 +22,7 @@ with any other potential backend.
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, cast, ClassVar, Type, Optional, Union from typing import TYPE_CHECKING, cast, Type, Optional, Union
import asyncio import asyncio
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import logging import logging
@ -35,7 +35,7 @@ from yarl import URL
from mautrix.util.logging import TraceLogger from mautrix.util.logging import TraceLogger
from ...config import Config from ...config import Config
from ...rpc import RPCClient from ...rpc import EventHandler, RPCClient
from ..types.api.struct.profile import ProfileReqStruct, ProfileStruct from ..types.api.struct.profile import ProfileReqStruct, ProfileStruct
from ..types.api.struct import FriendListStruct from ..types.api.struct import FriendListStruct
@ -136,11 +136,13 @@ class Client:
_rpc_disconnection_task: asyncio.Task | None _rpc_disconnection_task: asyncio.Task | None
http: ClientSession http: ClientSession
log: TraceLogger log: TraceLogger
_handler_methods: list[str]
def __init__(self, user: u.User, log: Optional[TraceLogger] = None): def __init__(self, user: u.User, log: Optional[TraceLogger] = None):
"""Create a per-user client object for user-specific client functionality.""" """Create a per-user client object for user-specific client functionality."""
self.user = user self.user = user
self._rpc_disconnection_task = None self._rpc_disconnection_task = None
self._handler_methods = []
# TODO Let the Node backend use a proxy too! # TODO Let the Node backend use a proxy too!
connector = None connector = None
@ -403,18 +405,19 @@ class Client:
def _start_listen(self) -> None: def _start_listen(self) -> None:
# TODO Automate this somehow, like with a fancy enum self._add_event_handler("chat", self._on_message)
self._rpc_client.set_event_handlers(self._get_user_cmd("chat"), [self._on_message])
# TODO many more listeners # TODO many more listeners
self._rpc_client.set_event_handlers(self._get_user_cmd("disconnected"), [self._on_listen_disconnect]) self._add_event_handler("disconnected", self._on_listen_disconnect)
self._rpc_client.set_event_handlers(self._get_user_cmd("switch_server"), [self._on_switch_server]) self._add_event_handler("switch_server", self._on_switch_server)
def _stop_listen(self) -> None: def _stop_listen(self) -> None:
# TODO Automate this somehow, like with a fancy enum for method in self._handler_methods:
self._rpc_client.set_event_handlers(self._get_user_cmd("chat"), []) self._rpc_client.set_event_handlers(self._get_user_cmd(method), [])
# TODO many more listeners
self._rpc_client.set_event_handlers(self._get_user_cmd("disconnected"), [])
self._rpc_client.set_event_handlers(self._get_user_cmd("switch_server"), []) def _add_event_handler(self, method: str, handler: EventHandler):
self._rpc_client.set_event_handlers(self._get_user_cmd(method), [handler])
self._handler_methods.append(method)
def _get_user_cmd(self, command) -> str: def _get_user_cmd(self, command) -> str:
return f"{command}:{self.user.mxid}" return f"{command}:{self.user.mxid}"

View File

@ -1,2 +1,2 @@
from .rpc import RPCClient from .rpc import RPCClient, EventHandler
from .types import RPCError from .types import RPCError