mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +03:00
Support asynchronous filters (#4434)
This commit is contained in:
parent
2ab4bed02d
commit
eb7ed5dd31
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
from inspect import isawaitable
|
||||
from typing import TYPE_CHECKING, Any, Optional, Sequence, Type, TypeVar
|
||||
|
||||
from ...session import Gap
|
||||
|
@ -23,7 +24,7 @@ def on(
|
|||
self: Client, event_cls: Type[Event], /, filter: Optional[Filter] = None
|
||||
) -> Callable[[Callable[[Event], Awaitable[Any]]], Callable[[Event], Awaitable[Any]]]:
|
||||
def wrapper(
|
||||
handler: Callable[[Event], Awaitable[Any]]
|
||||
handler: Callable[[Event], Awaitable[Any]],
|
||||
) -> Callable[[Event], Awaitable[Any]]:
|
||||
add_event_handler(self, handler, event_cls, filter)
|
||||
return handler
|
||||
|
@ -145,7 +146,7 @@ async def dispatch_next(client: Client) -> None:
|
|||
for event_cls, handlers in client._handlers.items():
|
||||
if event := event_cls._try_from_update(client, update, chat_map):
|
||||
for handler, filter in handlers:
|
||||
if not filter or filter(event):
|
||||
if not filter or (await r if isawaitable(r := filter(event)) else r):
|
||||
ret = await handler(event)
|
||||
if not (ret is Continue or client._check_all_handlers):
|
||||
return
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import abc
|
||||
import typing
|
||||
from collections.abc import Callable
|
||||
from typing import TypeAlias
|
||||
from typing import Awaitable, TypeAlias
|
||||
|
||||
from ..event import Event
|
||||
|
||||
Filter: TypeAlias = Callable[[Event], bool]
|
||||
Filter: TypeAlias = Callable[[Event], bool | Awaitable[bool]]
|
||||
|
||||
|
||||
class Combinable(abc.ABC):
|
||||
|
|
Loading…
Reference in New Issue
Block a user