Avoid unnecessary await in Conversation

This commit is contained in:
Lonami Exo 2019-08-13 18:11:02 +02:00
parent e24dd3ad75
commit 61c0e63bbe
2 changed files with 14 additions and 11 deletions

View File

@ -63,6 +63,10 @@ syncify(TelegramClient, _TakeoutClient, Draft, Dialog, MessageButton,
ChatGetter, SenderGetter, Forward, Message, InlineResult, Conversation) ChatGetter, SenderGetter, Forward, Message, InlineResult, Conversation)
# Private special case, since a conversation's methods return
# futures (but the public function themselves are synchronous).
_syncify_wrap(Conversation, '_get_result')
__all__ = [ __all__ = [
'TelegramClient', 'Button', 'TelegramClient', 'Button',
'types', 'functions', 'custom', 'errors', 'types', 'functions', 'custom', 'errors',

View File

@ -4,7 +4,6 @@ import time
from .chatgetter import ChatGetter from .chatgetter import ChatGetter
from ... import helpers, utils, errors from ... import helpers, utils, errors
from ...events.common import EventCommon
# Sometimes the edits arrive very fast (within the same second). # Sometimes the edits arrive very fast (within the same second).
# In that case we add a small delta so that the age is older, for # In that case we add a small delta so that the age is older, for
@ -112,7 +111,7 @@ class Conversation(ChatGetter):
return self._client.send_read_acknowledge( return self._client.send_read_acknowledge(
self._input_chat, max_id=message) self._input_chat, max_id=message)
async def get_response(self, message=None, *, timeout=None): def get_response(self, message=None, *, timeout=None):
""" """
Gets the next message that responds to a previous one. Gets the next message that responds to a previous one.
@ -125,16 +124,16 @@ class Conversation(ChatGetter):
If present, this `timeout` (in seconds) will override the If present, this `timeout` (in seconds) will override the
per-action timeout defined for the conversation. per-action timeout defined for the conversation.
""" """
return await self._get_message( return self._get_message(
message, self._response_indices, self._pending_responses, timeout, message, self._response_indices, self._pending_responses, timeout,
lambda x, y: True lambda x, y: True
) )
async def get_reply(self, message=None, *, timeout=None): def get_reply(self, message=None, *, timeout=None):
""" """
Gets the next message that explicitly replies to a previous one. Gets the next message that explicitly replies to a previous one.
""" """
return await self._get_message( return self._get_message(
message, self._reply_indices, self._pending_replies, timeout, message, self._reply_indices, self._pending_replies, timeout,
lambda x, y: x.reply_to_msg_id == y lambda x, y: x.reply_to_msg_id == y
) )
@ -200,7 +199,7 @@ class Conversation(ChatGetter):
pending[target_id] = future pending[target_id] = future
return self._get_result(future, start_time, timeout, pending, target_id) return self._get_result(future, start_time, timeout, pending, target_id)
async def get_edit(self, message=None, *, timeout=None): def get_edit(self, message=None, *, timeout=None):
""" """
Awaits for an edit after the last message to arrive. Awaits for an edit after the last message to arrive.
The arguments are the same as those for `get_response`. The arguments are the same as those for `get_response`.
@ -226,9 +225,9 @@ class Conversation(ChatGetter):
# Otherwise the next incoming response will be the one to use # Otherwise the next incoming response will be the one to use
future = self._client.loop.create_future() future = self._client.loop.create_future()
self._pending_edits[target_id] = future self._pending_edits[target_id] = future
return await self._get_result(future, start_time, timeout, self._pending_edits, target_id) return self._get_result(future, start_time, timeout, self._pending_edits, target_id)
async def wait_read(self, message=None, *, timeout=None): def wait_read(self, message=None, *, timeout=None):
""" """
Awaits for the sent message to be marked as read. Note that Awaits for the sent message to be marked as read. Note that
receiving a response doesn't imply the message was read, and receiving a response doesn't imply the message was read, and
@ -245,7 +244,7 @@ class Conversation(ChatGetter):
return return
self._pending_reads[target_id] = future self._pending_reads[target_id] = future
return await self._get_result(future, start_time, timeout, self._pending_reads, target_id) return self._get_result(future, start_time, timeout, self._pending_reads, target_id)
async def wait_event(self, event, *, timeout=None): async def wait_event(self, event, *, timeout=None):
""" """
@ -363,7 +362,7 @@ class Conversation(ChatGetter):
else: else:
raise ValueError('No message was sent previously') raise ValueError('No message was sent previously')
async def _get_result(self, future, start_time, timeout, pending, target_id): def _get_result(self, future, start_time, timeout, pending, target_id):
if self._cancelled: if self._cancelled:
raise asyncio.CancelledError('The conversation was cancelled before') raise asyncio.CancelledError('The conversation was cancelled before')
@ -379,7 +378,7 @@ class Conversation(ChatGetter):
# dispatch another update before, and in that case a # dispatch another update before, and in that case a
# response could be set twice. So responses must be # response could be set twice. So responses must be
# cleared when their futures are set to a result. # cleared when their futures are set to a result.
return await asyncio.wait_for( return asyncio.wait_for(
future, future,
timeout=None if due == float('inf') else due - time.time(), timeout=None if due == float('inf') else due - time.time(),
loop=self._client.loop loop=self._client.loop