Added an event hook priority system

This commit is contained in:
jesopo 2018-07-02 12:23:33 +01:00
parent f6a5d97842
commit 035d62f9d4

View file

@ -16,9 +16,15 @@ class Event(object):
self.eaten = True self.eaten = True
class EventCallback(object): class EventCallback(object):
def __init__(self, function, bot, **kwargs): PRIORITY_URGENT = 0
PRIORITY_HIGH = 1
PRIORITY_MEDIUM = 2
PRIORITY_LOW = 3
def __init__(self, function, bot, priority, **kwargs):
self.function = function self.function = function
self.bot = bot self.bot = bot
self.priority = priority
self.kwargs = kwargs self.kwargs = kwargs
def call(self, event): def call(self, event):
return self.function(event) return self.function(event)
@ -45,11 +51,13 @@ class EventHook(object):
self._child_notify = None self._child_notify = None
self._call_notify = None self._call_notify = None
self._stored_events = [] self._stored_events = []
def hook(self, function, replay=False, **kwargs): def hook(self, function, priority=EventCallback.PRIORITY_LOW,
callback = EventCallback(function, self.bot, **kwargs) replay=False, **kwargs):
callback = EventCallback(function, self.bot, priority, **kwargs)
if self._hook_notify: if self._hook_notify:
self._hook_notify(self, callback) self._hook_notify(self, callback)
self._hooks.append(callback) self._hooks.append(callback)
self._hooks.sort(key=lambda x: x.priority)
if replay: if replay:
for event in self._stored_events: for event in self._stored_events: