diff options
Diffstat (limited to 'poezio/events.py')
-rw-r--r-- | poezio/events.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/poezio/events.py b/poezio/events.py index 5213f663..0ba97d56 100644 --- a/poezio/events.py +++ b/poezio/events.py @@ -2,16 +2,20 @@ # This file is part of Poezio. # # Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. +# it under the terms of the GPL-3.0+ license. See the COPYING file. """ 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: """ @@ -48,7 +52,7 @@ class EventHandler: 'ignored_private', 'tab_change', ] - self.events = {} # type: Dict[str, OrderedDict[int, List[Callable]]] + self.events: Dict[str, OrderedDict[int, List[Callable]]] = {} for event in events: self.events[event] = OrderedDict() @@ -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): """ |