Visitar URL original
Add update handlers for connected, disconnected and ready by alemidev · Pull Request #695 · pyrogram/pyrogram · GitHub
The Wayback Machine - https://web.archive.org/web/20240112140724/https://github.com/pyrogram/pyrogram/pull/695
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update handlers for connected, disconnected and ready #695

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

alemidev
Copy link

@alemidev alemidev commented Jun 11, 2021 •

Added
@Client.on_ready()
@Client.on_connected()
@Client.on_disconnected()
decorators (and related Handlers)

Update: it's now just @Client.on_client_status

These events can be registered from smart plugins and can be grouped (unlike how DisconnectHandler was implemented).
To achieve this, "fake" updates are pushed on dispatcher's updates_queue. These are special "sentinel" classes defined in pyrogram.session.session.py and hold almost no information.

I had to map these handlers in the dispatcher. Also, the dispatcher used to just make a tuple of parsed_update (the object given out by the update parser). Since these updates hold no information to parse, they don't need any other argument in the callback other than client. To allow this, now parsed_update is checked, and if not iterable, a tuple is made. Like this, parsers can return iterables with multiple args (or none).

I'm not sure how docs are generated? I added the new methods and object inside the proper .rst files.

alemi and others added 10 commits June 11, 2021 13:15
Improved Disconnect Handler: now can be registered from smart plugins
and can be more than one.

Added Connect Handler: same thing as Disconnect Handler but for connections.

Added Client Ready Handler: fired once when client is ready first time.

To implement these, I added some "sentinel" objects in session.py, which
get put on the updates_queue. They hold no info and are just placeholders,
so that the dispatcher can recognize them annd fire the proper handlers.
on_connect -> on_connected
on_disconnect -> on_disconnected
@alemidev
Copy link
Author

Updated: there's only one handler + decorator now: @Client.on_client_status. There are filters to only register to certain events:
@Client.on_client_status(filters.client_ready) is @Client.on_ready()
@Client.on_client_status(filters.client_disconnected) is @Client.on_disconnected()

@alemidev
Copy link
Author

alemidev commented Jan 3, 2022

@sam01101 Commits can (and should) be squashed at merge time. I don't see why I should hide my progress (and mistakes!) from the PR itself

@delivrance
Copy link
Member

Having handlers/decorators for such events is useful, but I don't like the implementation proposed here, especially because you merged them with the actual update dispatcher and filters.
I also prefer sticking with separate decorators because that's how it is now for all other decorators. Decorators should also have different names to reflect the method that trigger them (i.e.: start, stop); so, something like on_starting (before start), on_started (after start), on_stopping (before stop), on_stopped (after stop).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants