From e7122e96ef8c1bb37ee498df7fcef2c0f8c81b1e Mon Sep 17 00:00:00 2001 From: Serhii Dylda Date: Thu, 8 Oct 2020 13:04:45 +0200 Subject: [PATCH] New: blocking version of catch_up() --- telethon/client/updates.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index e649b683..6e52439c 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -279,6 +279,30 @@ class UpdateMethods: # endregion + async def fetch_updates(self: 'TelegramClient'): + """ + 'Synchronous' version of 'catch_up()'. Fetch updates and + wait until all of them are processed. + + Additional sanity check for update State. + If session missing update state we fetch the latest one. + + Example + .. code-block:: python + + await client.fetch_updates() + + """ + update_state = self.session.get_update_state(0) + if not update_state or update_state.pts == 0: + state = await self(functions.updates.GetStateRequest()) + self.session.set_update_state(0, state) + + await self.catch_up() + while self._updates_queue: + await asyncio.wait(self._updates_queue, return_when=asyncio.ALL_COMPLETED) + + # region Private methods # It is important to not make _handle_update async because we rely on