From 075bf3e60f9c38d8594f8db71354a984faf9a70b Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 9 Apr 2022 03:04:00 -0400 Subject: [PATCH] Better tracking of client event handlers --- .../kt/client/client.py | 25 +++++++++++-------- matrix_appservice_kakaotalk/rpc/__init__.py | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index dfea810..febd26f 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -22,7 +22,7 @@ with any other potential backend. 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 from contextlib import asynccontextmanager import logging @@ -35,7 +35,7 @@ from yarl import URL from mautrix.util.logging import TraceLogger 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 import FriendListStruct @@ -136,11 +136,13 @@ class Client: _rpc_disconnection_task: asyncio.Task | None http: ClientSession log: TraceLogger + _handler_methods: list[str] def __init__(self, user: u.User, log: Optional[TraceLogger] = None): """Create a per-user client object for user-specific client functionality.""" self.user = user self._rpc_disconnection_task = None + self._handler_methods = [] # TODO Let the Node backend use a proxy too! connector = None @@ -403,18 +405,19 @@ class Client: def _start_listen(self) -> None: - # TODO Automate this somehow, like with a fancy enum - self._rpc_client.set_event_handlers(self._get_user_cmd("chat"), [self._on_message]) + self._add_event_handler("chat", self._on_message) # TODO many more listeners - self._rpc_client.set_event_handlers(self._get_user_cmd("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("disconnected", self._on_listen_disconnect) + self._add_event_handler("switch_server", self._on_switch_server) def _stop_listen(self) -> None: - # TODO Automate this somehow, like with a fancy enum - self._rpc_client.set_event_handlers(self._get_user_cmd("chat"), []) - # 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"), []) + for method in self._handler_methods: + self._rpc_client.set_event_handlers(self._get_user_cmd(method), []) + + + 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: return f"{command}:{self.user.mxid}" diff --git a/matrix_appservice_kakaotalk/rpc/__init__.py b/matrix_appservice_kakaotalk/rpc/__init__.py index f5125b1..8076150 100644 --- a/matrix_appservice_kakaotalk/rpc/__init__.py +++ b/matrix_appservice_kakaotalk/rpc/__init__.py @@ -1,2 +1,2 @@ -from .rpc import RPCClient +from .rpc import RPCClient, EventHandler from .types import RPCError