From 833cccb188967948bc786fa6e9385821f8676362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 7 Jun 2019 14:12:19 +0200 Subject: Rework priority handling for events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce the concept of priority for event handlers, instead of the position parameter. The new `priority` parameter replacing `position` should be an integer between 0 and 100. It defaults to 50. The previous `position` parameter was only used to insert at a certain position in the list of handlers (for this particular event). No reference of it was kept, which means that it was not possible to ensure an event was called in the position is was supposed to be. I am now using per-event dicts, containing priority buckets (lists) of handlers. I am using OrderedDicts to make it easier to loop through all of the handlers, as insertion happens less often than reading. I was also suggested using bisect with a simple list of tuples (priority, handler) per event, but bisect tries to compare bound methods, which is obviously not possible. Maybe it would be interesting to find a way use this method instead of OrderedDict as that might be less resource consuming. This said, I don't think this part of poezio is a bottleneck at all, so maybe this is just fine as is. Signed-off-by: Maxime “pep” Buquet --- poezio/events.py | 89 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 38 deletions(-) (limited to 'poezio/events.py') diff --git a/poezio/events.py b/poezio/events.py index 3bfe5156..b34eef32 100644 --- a/poezio/events.py +++ b/poezio/events.py @@ -9,6 +9,7 @@ The list of available events is here: http://poezio.eu/doc/en/plugins.html#_poezio_events """ +from collections import OrderedDict from typing import Callable, Dict, List @@ -21,49 +22,56 @@ class EventHandler: """ def __init__(self): - self.events = { - 'highlight': [], - 'muc_say': [], - 'muc_say_after': [], - 'conversation_say': [], - 'conversation_say_after': [], - 'private_say': [], - 'private_say_after': [], - 'conversation_msg': [], - 'private_msg': [], - 'muc_msg': [], - 'conversation_chatstate': [], - 'muc_chatstate': [], - 'private_chatstate': [], - 'normal_presence': [], - 'muc_presence': [], - 'muc_join': [], - 'joining_muc': [], - 'changing_nick': [], - 'muc_kick': [], - 'muc_nickchange': [], - 'muc_ban': [], - 'send_normal_presence': [], - 'ignored_private': [], - 'tab_change': [], - } # type: Dict[str, List[Callable]] + events = [ + 'highlight', + 'muc_say', + 'muc_say_after', + 'conversation_say', + 'conversation_say_after', + 'private_say', + 'private_say_after', + 'conversation_msg', + 'private_msg', + 'muc_msg', + 'conversation_chatstate', + 'muc_chatstate', + 'private_chatstate', + 'normal_presence', + 'muc_presence', + 'muc_join', + 'joining_muc', + 'changing_nick', + 'muc_kick', + 'muc_nickchange', + 'muc_ban', + 'send_normal_presence', + 'ignored_private', + 'tab_change', + ] + self.events = {} # type: Dict[str, OrderedDict[int, List[Callable]]] + for event in events: + self.events[event] = OrderedDict() def add_event_handler(self, name: str, callback: Callable, - position=0) -> bool: + priority: int = 50) -> bool: """ Add a callback to a given event. Note that if that event name doesn’t exist, it just returns False. If it was successfully added, it returns True - position: 0 means insert at the beginning, -1 means end + priority is a integer between 0 and 100. 0 is the highest priority and + will be called first. 100 is the lowest. """ + if name not in self.events: return False callbacks = self.events[name] - if position >= 0: - callbacks.insert(position, callback) - else: - callbacks.append(callback) + + # Clamp priority + priority = max(0, min(priority, 100)) + + entry = callbacks.setdefault(priority, []) + entry.append(callback) return True @@ -74,8 +82,9 @@ class EventHandler: callbacks = self.events.get(name, None) if callbacks is None: return - for callback in callbacks: - callback(*args, **kwargs) + for priority in callbacks.values(): + for callback in priority: + callback(*args, **kwargs) def del_event_handler(self, name: str, callback: Callable): """ @@ -83,9 +92,13 @@ class EventHandler: """ if not name: for callbacks in self.events.values(): - while callback in callbacks: - callbacks.remove(callback) + for priority in callbacks.values(): + for entry in priority[:]: + if entry == callback: + priority.remove(callback) else: callbacks = self.events[name] - if callback in callbacks: - callbacks.remove(callback) + for priority in callbacks.entries(): + for entry in priority[:]: + if entry == callback: + priority.remove(callback) -- cgit v1.2.3