summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2022-02-06 18:54:50 +0100
committermathieui <mathieui@mathieui.net>2022-02-10 16:32:30 +0100
commit22093250ef543dfd68fc31500b39834ab5143704 (patch)
tree71e318e945da3e454942910928c72183bbff7a4a
parentd35c0564b3f5dd10f6e23bf462bbfabd3084e486 (diff)
downloadpoezio-22093250ef543dfd68fc31500b39834ab5143704.tar.gz
poezio-22093250ef543dfd68fc31500b39834ab5143704.tar.bz2
poezio-22093250ef543dfd68fc31500b39834ab5143704.tar.xz
poezio-22093250ef543dfd68fc31500b39834ab5143704.zip
internal: add a trigger_async method for events
-rw-r--r--poezio/events.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/poezio/events.py b/poezio/events.py
index 63782836..a846d38d 100644
--- a/poezio/events.py
+++ b/poezio/events.py
@@ -8,10 +8,14 @@ Defines the EventHandler class.
The list of available events is here:
http://poezio.eu/doc/en/plugins.html#_poezio_events
"""
+import logging
from collections import OrderedDict
+from inspect import iscoroutinefunction
from typing import Callable, Dict, List
+log = logging.getLogger(__name__)
+
class EventHandler:
"""
@@ -75,6 +79,20 @@ class EventHandler:
return True
+ async def trigger_async(self, name: str, *args, **kwargs):
+ """
+ Call all the callbacks associated to the given event name.
+ """
+ callbacks = self.events.get(name, None)
+ if callbacks is None:
+ return
+ for priority in callbacks.values():
+ for callback in priority:
+ if iscoroutinefunction(callback):
+ await callback(*args, **kwargs)
+ else:
+ callback(*args, **kwargs)
+
def trigger(self, name: str, *args, **kwargs):
"""
Call all the callbacks associated to the given event name.
@@ -84,7 +102,11 @@ class EventHandler:
return
for priority in callbacks.values():
for callback in priority:
- callback(*args, **kwargs)
+ if not iscoroutinefunction(callback):
+ callback(*args, **kwargs)
+ else:
+ log.error(f'async event handler {callback} '
+ 'called in sync trigger!')
def del_event_handler(self, name: str, callback: Callable):
"""