Better tracking of client event handlers
This commit is contained in:
parent
aee66976f6
commit
075bf3e60f
@ -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}"
|
||||
|
@ -1,2 +1,2 @@
|
||||
from .rpc import RPCClient
|
||||
from .rpc import RPCClient, EventHandler
|
||||
from .types import RPCError
|
||||
|
Loading…
Reference in New Issue
Block a user