Skip to content

Commit 31c5bf4

Browse files
committed
simplify
1 parent 34d5d2c commit 31c5bf4

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

livekit-rtc/livekit/rtc/participant.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ def disconnect_reason(
145145

146146

147147
RpcHandler = Callable[["RpcInvocationData"], Union[Awaitable[Optional[str]], Optional[str]]]
148-
F = TypeVar("F", bound=RpcHandler)
148+
F = TypeVar(
149+
"F", bound=Callable[[RpcInvocationData], Union[Awaitable[Optional[str]], Optional[str]]]
150+
)
149151

150152

151153
class LocalParticipant(Participant):
@@ -159,7 +161,7 @@ def __init__(
159161
super().__init__(owned_info)
160162
self._room_queue = room_queue
161163
self._track_publications: dict[str, LocalTrackPublication] = {} # type: ignore
162-
self._rpc_handlers: Dict[str, RpcHandler] = {}
164+
self._rpc_handlers: Dict[str, F] = {}
163165

164166
@property
165167
def track_publications(self) -> Mapping[str, LocalTrackPublication]:
@@ -439,26 +441,16 @@ async def _handle_rpc_method_invocation(
439441
else:
440442
try:
441443
if asyncio.iscoroutinefunction(handler):
442-
async_handler = cast(Callable[[RpcInvocationData], Awaitable[str]], handler)
443-
444-
async def run_handler():
445-
try:
446-
return await async_handler(params)
447-
except asyncio.CancelledError:
448-
# This will be caught by the outer try-except if it's due to timeout
449-
raise
450-
451444
try:
452445
response_payload = await asyncio.wait_for(
453-
run_handler(), timeout=response_timeout
446+
handler(params), timeout=response_timeout
454447
)
455448
except asyncio.TimeoutError:
456449
raise RpcError._built_in(RpcError.ErrorCode.RESPONSE_TIMEOUT)
457450
except asyncio.CancelledError:
458451
raise RpcError._built_in(RpcError.ErrorCode.RECIPIENT_DISCONNECTED)
459452
else:
460-
sync_handler = cast(Callable[[RpcInvocationData], str], handler)
461-
response_payload = sync_handler(params)
453+
response_payload = cast(Optional[str], handler(params))
462454
except RpcError as error:
463455
response_error = error
464456
except Exception as error:

0 commit comments

Comments
 (0)