Skip to content

Commit 34d5d2c

Browse files
committed
Update participant.py
1 parent 61c3db8 commit 34d5d2c

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

livekit-rtc/livekit/rtc/participant.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020
import mimetypes
2121
import aiofiles
22-
from typing import List, Union, Callable, Dict, Awaitable, Optional, Mapping, cast
22+
from typing import List, Union, Callable, Dict, Awaitable, Optional, Mapping, cast, TypeVar
2323
from abc import abstractmethod, ABC
2424

2525
from ._ffi_client import FfiClient, FfiHandle
@@ -144,6 +144,10 @@ def disconnect_reason(
144144
return self._info.disconnect_reason
145145

146146

147+
RpcHandler = Callable[["RpcInvocationData"], Union[Awaitable[Optional[str]], Optional[str]]]
148+
F = TypeVar("F", bound=RpcHandler)
149+
150+
147151
class LocalParticipant(Participant):
148152
"""Represents the local participant in a room."""
149153

@@ -155,9 +159,7 @@ def __init__(
155159
super().__init__(owned_info)
156160
self._room_queue = room_queue
157161
self._track_publications: dict[str, LocalTrackPublication] = {} # type: ignore
158-
self._rpc_handlers: Dict[
159-
str, Callable[[RpcInvocationData], Union[Awaitable[str], str]]
160-
] = {}
162+
self._rpc_handlers: Dict[str, RpcHandler] = {}
161163

162164
@property
163165
def track_publications(self) -> Mapping[str, LocalTrackPublication]:
@@ -328,8 +330,8 @@ async def perform_rpc(
328330
def register_rpc_method(
329331
self,
330332
method_name: str,
331-
handler: Optional[Callable[[RpcInvocationData], Union[Awaitable[str], str]]] = None,
332-
) -> Union[None, Callable]:
333+
handler: Optional[F] = None,
334+
) -> Union[F, Callable[[F], F]]:
333335
"""
334336
Establishes the participant as a receiver for calls of the specified RPC method.
335337
Can be used either as a decorator or a regular method.
@@ -366,18 +368,17 @@ async def greet_handler(data: RpcInvocationData) -> str:
366368
room.local_participant.register_rpc_method('greet', greet_handler)
367369
"""
368370

369-
def register(handler_func):
371+
def register(handler_func: F) -> F:
370372
self._rpc_handlers[method_name] = handler_func
371373
req = proto_ffi.FfiRequest()
372374
req.register_rpc_method.local_participant_handle = self._ffi_handle.handle
373375
req.register_rpc_method.method = method_name
374376
FfiClient.instance.request(req)
377+
return handler_func
375378

376379
if handler is not None:
377-
register(handler)
378-
return None
380+
return register(handler)
379381
else:
380-
# Called as a decorator
381382
return register
382383

383384
def unregister_rpc_method(self, method: str) -> None:

0 commit comments

Comments
 (0)