mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-06 21:20:22 +03:00
Added StopPropagation logic
This commit is contained in:
parent
4050d1ca00
commit
dc7a6fcce3
|
@ -121,6 +121,30 @@ random number, while if you say ``'eval 4+4'``, you will reply with the
|
||||||
solution. Try it!
|
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
|
Events module
|
||||||
*************
|
*************
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,7 @@ from mimetypes import guess_type
|
||||||
|
|
||||||
from .crypto import CdnDecrypter
|
from .crypto import CdnDecrypter
|
||||||
from .tl.custom import InputSizedFile
|
from .tl.custom import InputSizedFile
|
||||||
from .tl.functions.upload import (
|
from .tl.functions.upload import (GetFileRequest, SaveBigFilePartRequest, SaveFilePartRequest)
|
||||||
SaveBigFilePartRequest, SaveFilePartRequest, GetFileRequest
|
|
||||||
)
|
|
||||||
from .tl.types.upload import FileCdnRedirect
|
from .tl.types.upload import FileCdnRedirect
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1768,6 +1766,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
The event builder class or instance to be used,
|
The event builder class or instance to be used,
|
||||||
for instance ``events.NewMessage``.
|
for instance ``events.NewMessage``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(f):
|
def decorator(f):
|
||||||
self.add_event_handler(f, event)
|
self.add_event_handler(f, event)
|
||||||
return f
|
return f
|
||||||
|
@ -1779,7 +1778,13 @@ class TelegramClient(TelegramBareClient):
|
||||||
event = builder.build(update)
|
event = builder.build(update)
|
||||||
if event:
|
if event:
|
||||||
event._client = self
|
event._client = self
|
||||||
|
try:
|
||||||
callback(event)
|
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):
|
def add_event_handler(self, callback, event):
|
||||||
"""
|
"""
|
||||||
|
@ -2014,3 +2019,30 @@ class TelegramClient(TelegramBareClient):
|
||||||
)
|
)
|
||||||
|
|
||||||
# endregion
|
# 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