Simplify event resolving logic

Although this commit introduces a race condition since an
event may only be half-resolved. A lock is thus needed,
but it depends on an event-loop to which we don't have
access in the class-level.
This commit is contained in:
Lonami Exo
2018-08-21 11:08:08 +02:00
parent 9f237cc928
commit d474458136
6 changed files with 26 additions and 31 deletions

View File

@@ -89,7 +89,7 @@ class UpdateMethods(UserMethods):
elif not event:
event = events.Raw()
self._events_pending_resolve.append(event)
self._loop.create_task(event.resolve(self))
self._event_builders.append((event, callback))
def remove_event_handler(self, callback, event=None):
@@ -249,17 +249,6 @@ class UpdateMethods(UserMethods):
self._dispatching_updates_queue.clear()
async def _dispatch_update(self, update):
if self._events_pending_resolve:
if self._event_resolve_lock.locked():
async with self._event_resolve_lock:
pass
else:
async with self._event_resolve_lock:
for event in self._events_pending_resolve:
await event.resolve(self)
self._events_pending_resolve.clear()
built = EventBuilderDict(self, update)
if self._conversations:
for conv in self._conversations.values():
@@ -274,7 +263,15 @@ class UpdateMethods(UserMethods):
for builder, callback in self._event_builders:
event = built[type(builder)]
if not event or not builder.filter(event):
if not event:
continue
# TODO Lock until it's resolved; the task for resolving
# was already created when adding the event handler.
if not builder.resolved:
await builder.resolve()
if not builder.filter(event):
continue
try: