mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Fix resolving events while disconnected
This commit is contained in:
parent
5ca1edb228
commit
576ac666d9
|
@ -89,7 +89,6 @@ class UpdateMethods(UserMethods):
|
|||
elif not event:
|
||||
event = events.Raw()
|
||||
|
||||
event.ensure_resolve(self)
|
||||
self._event_builders.append((event, callback))
|
||||
|
||||
def remove_event_handler(self, callback, event=None):
|
||||
|
@ -266,8 +265,8 @@ class UpdateMethods(UserMethods):
|
|||
if not event:
|
||||
continue
|
||||
|
||||
if not builder.resolved.is_set():
|
||||
await builder.resolved.wait()
|
||||
if not builder.resolved:
|
||||
await builder.resolve()
|
||||
|
||||
if not builder.filter(event):
|
||||
continue
|
||||
|
|
|
@ -58,38 +58,32 @@ class EventBuilder(abc.ABC):
|
|||
def __init__(self, chats=None, blacklist_chats=False):
|
||||
self.chats = chats
|
||||
self.blacklist_chats = blacklist_chats
|
||||
self.resolved = None
|
||||
self.resolved = False
|
||||
self._resolve_lock = None
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def build(cls, update):
|
||||
"""Builds an event for the given update if possible, or returns None"""
|
||||
|
||||
def ensure_resolve(self, client):
|
||||
"""
|
||||
Sets the event loop so that self.resolved can be used.
|
||||
|
||||
The expected workflow is:
|
||||
1. Creating the event builder.
|
||||
2a. Calling `ensure_resolve`.
|
||||
2b. Awaiting `resolved.wait`.
|
||||
OR
|
||||
2a. Awaiting `resolve`.
|
||||
3. Using `filter`.
|
||||
"""
|
||||
if not self.resolved:
|
||||
self.resolved = asyncio.Event(loop=client.loop)
|
||||
client.loop.create_task(self.resolve(client))
|
||||
|
||||
async def resolve(self, client):
|
||||
"""Helper method to allow event builders to be resolved before usage"""
|
||||
if not self.resolved.is_set():
|
||||
if self.resolved:
|
||||
return
|
||||
|
||||
if not self._resolve_lock:
|
||||
self._resolve_lock = asyncio.Lock(loop=client.loop)
|
||||
|
||||
async with self._resolve_lock:
|
||||
if not self.resolved:
|
||||
await self._resolve(client)
|
||||
self.resolved = True
|
||||
|
||||
async def _resolve(self, client):
|
||||
self.chats = await _into_id_set(client, self.chats)
|
||||
if not EventBuilder.self_id:
|
||||
EventBuilder.self_id = await client.get_peer_id('me')
|
||||
|
||||
self.resolved.set()
|
||||
|
||||
def filter(self, event):
|
||||
"""
|
||||
If the ID of ``event._chat_peer`` isn't in the chats set (or it is
|
||||
|
|
|
@ -71,10 +71,9 @@ class NewMessage(EventBuilder):
|
|||
self.from_users, self.forwards, self.from_users
|
||||
))
|
||||
|
||||
async def resolve(self, client):
|
||||
if not self.resolved.is_set():
|
||||
async def _resolve(self, client):
|
||||
await super()._resolve(client)
|
||||
self.from_users = await _into_id_set(client, self.from_users)
|
||||
await super().resolve(client)
|
||||
|
||||
@classmethod
|
||||
def build(cls, update):
|
||||
|
|
Loading…
Reference in New Issue
Block a user