mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-05 20:50:22 +03:00
Added StopPropagation logic
This commit is contained in:
parent
4050d1ca00
commit
dc7a6fcce3
|
@ -121,11 +121,35 @@ random number, while if you say ``'eval 4+4'``, you will reply with the
|
|||
solution. Try it!
|
||||
|
||||
|
||||
Stopping propagation of Updates
|
||||
*******************************
|
||||
|
||||
There might be cases when an event handler is supposed to be used solitary and
|
||||
it makes no sense to process any other handlers in the chain. For this case,
|
||||
it is possible to raise a ``StopPropagation`` exception which will cause the
|
||||
propagation of the update through your handlers to stop:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@client.on(events.NewMessage)
|
||||
def _(event):
|
||||
# ... some conditions
|
||||
event.delete()
|
||||
|
||||
# Other handlers won't have an event to work with
|
||||
raise client.StopPropagation
|
||||
|
||||
@client.on(events.NewMessage)
|
||||
def _(event):
|
||||
pass # Will never be reached, because
|
||||
# it is the second handler in the chain.
|
||||
|
||||
|
||||
Events module
|
||||
*************
|
||||
|
||||
.. automodule:: telethon.events
|
||||
:members:
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
|
|
|
@ -15,9 +15,7 @@ from mimetypes import guess_type
|
|||
|
||||
from .crypto import CdnDecrypter
|
||||
from .tl.custom import InputSizedFile
|
||||
from .tl.functions.upload import (
|
||||
SaveBigFilePartRequest, SaveFilePartRequest, GetFileRequest
|
||||
)
|
||||
from .tl.functions.upload import (GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest)
|
||||
from .tl.types.upload import FileCdnRedirect
|
||||
|
||||
try:
|
||||
|
@ -1768,6 +1766,7 @@ class TelegramClient(TelegramBareClient):
|
|||
The event builder class or instance to be used,
|
||||
for instance ``events.NewMessage``.
|
||||
"""
|
||||
|
||||
def decorator(f):
|
||||
self.add_event_handler(f, event)
|
||||
return f
|
||||
|
@ -1779,7 +1778,13 @@ class TelegramClient(TelegramBareClient):
|
|||
event = builder.build(update)
|
||||
if event:
|
||||
event._client = self
|
||||
try:
|
||||
callback(event)
|
||||
except StopPropagation:
|
||||
__log__.info("Event handler {0} stopped propagation of updates while processing {1}.".format(
|
||||
callback.__name__,
|
||||
update))
|
||||
break
|
||||
|
||||
def add_event_handler(self, callback, event):
|
||||
"""
|
||||
|
@ -2014,3 +2019,30 @@ class TelegramClient(TelegramBareClient):
|
|||
)
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Exceptions
|
||||
|
||||
class StopPropagation(Exception):
|
||||
"""
|
||||
If this Exception is found to be raised in any of the handlers for a
|
||||
given update, it will stop the execution of all other registered
|
||||
event handlers in the chain.
|
||||
Think of it like a ``StopIteration`` exception in a for loop.
|
||||
|
||||
Example usage:
|
||||
```
|
||||
@client.on(events.NewMessage)
|
||||
def delete(event):
|
||||
event.delete()
|
||||
# Other handlers won't have an event to work with
|
||||
raise StopPropagation
|
||||
|
||||
@client.on(events.NewMessage)
|
||||
def _(event):
|
||||
pass # Will never be reached, because
|
||||
# it is the second handler in the chain.
|
||||
```
|
||||
"""
|
||||
|
||||
# endregion
|
||||
|
|
Loading…
Reference in New Issue
Block a user