Skip to content

Commit a6dbed6

Browse files
committed
Add a way to continue the update propagation within a group
Add continue_propagation() method and ContinuePropagation exception Closes #212
1 parent ccecbd6 commit a6dbed6

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

docs/source/resources/MoreOnUpdates.rst

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ Stop Propagation
9191
In order to prevent further propagation of an update in the dispatching phase, you can do *one* of the following:
9292

9393
- Call the update's bound-method ``.stop_propagation()`` (preferred way).
94-
- Manually ``raise StopPropagation`` error (more suitable for raw updates only).
94+
- Manually ``raise StopPropagation`` exception (more suitable for raw updates only).
9595

9696
.. note::
9797

98-
Note that ``.stop_propagation()`` is just an elegant and intuitive way to raise a ``StopPropagation`` error;
99-
this means that any code coming *after* calling it won't be executed as your function just raised a custom exception
100-
to signal the dispatcher not to propagate the update anymore.
98+
Internally, the propagation is stopped by handling a custom exception. ``.stop_propagation()`` is just an elegant
99+
and intuitive way to ``raise StopPropagation``; this also means that any code coming *after* calling the method
100+
won't be executed as your function just raised an exception to signal the dispatcher not to propagate the
101+
update anymore.
101102

102103
Example with ``stop_propagation()``:
103104

@@ -139,10 +140,82 @@ Example with ``raise StopPropagation``:
139140
def _(client, message):
140141
print(2)
141142
142-
The handler in group number 2 will never be executed because the propagation was stopped before. The output of both
143-
examples will be:
143+
Each handler is registered in a different group, but the handler in group number 2 will never be executed because the
144+
propagation was stopped earlier. The output of both (equivalent) examples will be:
144145

145146
.. code-block:: text
146147
147148
0
148149
1
150+
151+
Continue Propagation
152+
^^^^^^^^^^^^^^^^^^^^
153+
154+
As opposed to `stopping the update propagation <#stop-propagation>`_ and also as an alternative to the
155+
`handler groups <#handler-groups>`_, you can signal the internal dispatcher to continue the update propagation within
156+
the group regardless of the next handler's filters. This allows you to register multiple handlers with overlapping
157+
filters in the same group; to let the dispatcher process the next handler you can do *one* of the following in each
158+
handler you want to grant permission to continue:
159+
160+
- Call the update's bound-method ``.continue_propagation()`` (preferred way).
161+
- Manually ``raise ContinuePropagation`` exception (more suitable for raw updates only).
162+
163+
.. note::
164+
165+
Internally, the propagation is continued by handling a custom exception. ``.continue_propagation()`` is just an
166+
elegant and intuitive way to ``raise ContinuePropagation``; this also means that any code coming *after* calling the
167+
method won't be executed as your function just raised an exception to signal the dispatcher to continue with the
168+
next available handler.
169+
170+
171+
Example with ``continue_propagation()``:
172+
173+
.. code-block:: python
174+
175+
@app.on_message(Filters.private)
176+
def _(client, message):
177+
print(0)
178+
message.continue_propagation()
179+
180+
181+
@app.on_message(Filters.private)
182+
def _(client, message):
183+
print(1)
184+
message.continue_propagation()
185+
186+
187+
@app.on_message(Filters.private)
188+
def _(client, message):
189+
print(2)
190+
191+
Example with ``raise ContinuePropagation``:
192+
193+
.. code-block:: python
194+
195+
from pyrogram import ContinuePropagation
196+
197+
@app.on_message(Filters.private)
198+
def _(client, message):
199+
print(0)
200+
raise ContinuePropagation
201+
202+
203+
@app.on_message(Filters.private)
204+
def _(client, message):
205+
print(1)
206+
raise ContinuePropagation
207+
208+
209+
@app.on_message(Filters.private)
210+
def _(client, message):
211+
print(2)
212+
213+
Three handlers are registered in the same group, and all of them will be executed because the propagation was continued
214+
in each handler (except in the last one, where is useless to do so since there is no more handlers after).
215+
The output of both (equivalent) examples will be:
216+
217+
.. code-block:: text
218+
219+
0
220+
1
221+
2

pyrogram/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, User, UserStatus,
3333
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
3434
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
35-
Poll, PollOption, ChatPreview, StopPropagation, Game, CallbackGame, GameHighScore, GameHighScores
35+
Poll, PollOption, ChatPreview, StopPropagation, ContinuePropagation, Game, CallbackGame, GameHighScore,
36+
GameHighScores
3637
)
3738
from .client import (
3839
Client, ChatAction, ParseMode, Emoji,

pyrogram/client/dispatcher/dispatcher.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def update_worker(self):
151151
handler.callback(self.client, *args)
152152
except pyrogram.StopPropagation:
153153
raise
154+
except pyrogram.ContinuePropagation:
155+
continue
154156
except Exception as e:
155157
log.error(e, exc_info=True)
156158

pyrogram/client/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Sticker, Venue, Video, VideoNote, Voice, UserProfilePhotos,
3131
Message, Messages, MessageEntity, Poll, PollOption, Game
3232
)
33-
from .update import StopPropagation
33+
from .update import StopPropagation, ContinuePropagation
3434
from .user_and_chats import (
3535
Chat, ChatMember, ChatMembers, ChatPhoto,
3636
Dialog, Dialogs, User, UserStatus, ChatPreview

pyrogram/client/types/update.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class StopPropagation(StopIteration):
2121
pass
2222

2323

24+
class ContinuePropagation(StopIteration):
25+
pass
26+
27+
2428
class Update:
2529
def stop_propagation(self):
2630
raise StopPropagation
31+
32+
def continue_propagation(self):
33+
raise ContinuePropagation

0 commit comments

Comments
 (0)