mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-05 22:23:15 +03:00
Make is_connected a property
This is consistent with the rest of is_ properties
This commit is contained in:
parent
483e2aadf1
commit
4258ce2bc8
|
@ -976,3 +976,5 @@ Now the URL is returned. You can still use ``webbrowser.open`` to get the old be
|
||||||
todo update send_message and send_file docs (well review all functions)
|
todo update send_message and send_file docs (well review all functions)
|
||||||
|
|
||||||
album overhaul. use a list of Message instead.
|
album overhaul. use a list of Message instead.
|
||||||
|
|
||||||
|
is_connected is now a property (consistent with the rest of ``is_`` properties)
|
||||||
|
|
|
@ -71,7 +71,7 @@ def start(
|
||||||
async def _start(
|
async def _start(
|
||||||
self: 'TelegramClient', phone, password, bot_token,
|
self: 'TelegramClient', phone, password, bot_token,
|
||||||
code_callback, first_name, last_name, max_attempts):
|
code_callback, first_name, last_name, max_attempts):
|
||||||
if not self.is_connected():
|
if not self.is_connected:
|
||||||
await self.connect()
|
await self.connect()
|
||||||
|
|
||||||
# Rather than using `is_user_authorized`, use `get_me`. While this is
|
# Rather than using `is_user_authorized`, use `get_me`. While this is
|
||||||
|
|
|
@ -308,12 +308,29 @@ async def connect(self: 'TelegramClient') -> None:
|
||||||
|
|
||||||
self._updates_handle = asyncio.create_task(self._update_loop())
|
self._updates_handle = asyncio.create_task(self._update_loop())
|
||||||
|
|
||||||
|
|
||||||
def is_connected(self: 'TelegramClient') -> bool:
|
def is_connected(self: 'TelegramClient') -> bool:
|
||||||
sender = getattr(self, '_sender', None)
|
return self._sender.is_connected()
|
||||||
return sender and sender.is_connected()
|
|
||||||
|
|
||||||
async def disconnect(self: 'TelegramClient'):
|
async def disconnect(self: 'TelegramClient'):
|
||||||
return await _disconnect_coro(self)
|
await _disconnect(self)
|
||||||
|
|
||||||
|
# Also clean-up all exported senders because we're done with them
|
||||||
|
async with self._borrow_sender_lock:
|
||||||
|
for state, sender in self._borrowed_senders.values():
|
||||||
|
# Note that we're not checking for `state.should_disconnect()`.
|
||||||
|
# If the user wants to disconnect the client, ALL connections
|
||||||
|
# to Telegram (including exported senders) should be closed.
|
||||||
|
#
|
||||||
|
# Disconnect should never raise, so there's no try/except.
|
||||||
|
await sender.disconnect()
|
||||||
|
# Can't use `mark_disconnected` because it may be borrowed.
|
||||||
|
state._connected = False
|
||||||
|
|
||||||
|
# If any was borrowed
|
||||||
|
self._borrowed_senders.clear()
|
||||||
|
|
||||||
|
|
||||||
def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]):
|
def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]):
|
||||||
init_proxy = None
|
init_proxy = None
|
||||||
|
@ -333,24 +350,6 @@ def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]):
|
||||||
else:
|
else:
|
||||||
connection._proxy = proxy
|
connection._proxy = proxy
|
||||||
|
|
||||||
async def _disconnect_coro(self: 'TelegramClient'):
|
|
||||||
await _disconnect(self)
|
|
||||||
|
|
||||||
# Also clean-up all exported senders because we're done with them
|
|
||||||
async with self._borrow_sender_lock:
|
|
||||||
for state, sender in self._borrowed_senders.values():
|
|
||||||
# Note that we're not checking for `state.should_disconnect()`.
|
|
||||||
# If the user wants to disconnect the client, ALL connections
|
|
||||||
# to Telegram (including exported senders) should be closed.
|
|
||||||
#
|
|
||||||
# Disconnect should never raise, so there's no try/except.
|
|
||||||
await sender.disconnect()
|
|
||||||
# Can't use `mark_disconnected` because it may be borrowed.
|
|
||||||
state._connected = False
|
|
||||||
|
|
||||||
# If any was borrowed
|
|
||||||
self._borrowed_senders.clear()
|
|
||||||
|
|
||||||
|
|
||||||
async def _disconnect(self: 'TelegramClient'):
|
async def _disconnect(self: 'TelegramClient'):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2637,7 +2637,7 @@ class TelegramClient:
|
||||||
print('Failed to connect')
|
print('Failed to connect')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@forward_call(telegrambaseclient.is_connected)
|
@property
|
||||||
def is_connected(self: 'TelegramClient') -> bool:
|
def is_connected(self: 'TelegramClient') -> bool:
|
||||||
"""
|
"""
|
||||||
Returns `True` if the user has connected.
|
Returns `True` if the user has connected.
|
||||||
|
@ -2647,9 +2647,11 @@ class TelegramClient:
|
||||||
Example
|
Example
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
while client.is_connected():
|
# This is a silly example - run_until_disconnected is often better suited
|
||||||
|
while client.is_connected:
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
"""
|
"""
|
||||||
|
return telegrambaseclient.is_connected(self)
|
||||||
|
|
||||||
@forward_call(telegrambaseclient.disconnect)
|
@forward_call(telegrambaseclient.disconnect)
|
||||||
def disconnect(self: 'TelegramClient'):
|
def disconnect(self: 'TelegramClient'):
|
||||||
|
|
|
@ -168,7 +168,7 @@ async def catch_up(self: 'TelegramClient'):
|
||||||
async def _update_loop(self: 'TelegramClient'):
|
async def _update_loop(self: 'TelegramClient'):
|
||||||
try:
|
try:
|
||||||
updates_to_dispatch = deque()
|
updates_to_dispatch = deque()
|
||||||
while self.is_connected():
|
while self.is_connected:
|
||||||
if updates_to_dispatch:
|
if updates_to_dispatch:
|
||||||
await _dispatch(self, *updates_to_dispatch.popleft())
|
await _dispatch(self, *updates_to_dispatch.popleft())
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -228,7 +228,7 @@ class App(tkinter.Tk):
|
||||||
"""
|
"""
|
||||||
Sends a message. Does nothing if the client is not connected.
|
Sends a message. Does nothing if the client is not connected.
|
||||||
"""
|
"""
|
||||||
if not self.cl.is_connected():
|
if not self.cl.is_connected:
|
||||||
return
|
return
|
||||||
|
|
||||||
# The user needs to configure a chat where the message should be sent.
|
# The user needs to configure a chat where the message should be sent.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user