Update Event to allow arbitrary keyword arguments

Comments are on the same line as the possibly-failing line; if it fails,
the traceback will include that line (so it will be obvious what the
error is without looking at the source).
This commit is contained in:
Vgr E. Barry 2016-10-25 21:25:33 -04:00
parent 0ad4af4240
commit 85972db5b1

View File

@ -15,18 +15,22 @@ def remove_listener(event, callback, priority = 5):
EVENT_CALLBACKS[event].remove((priority, callback))
class Event:
def __init__(self, name, data, **kwargs):
def __init__(*args, **kwargs):
# We do this so that e.g. 'data' can be given as a param
self, name, data = args # If this raises, there are too few/many args
self.stop_processing = False
self.prevent_default = False
self.name = name
self.data = data
self.params = SimpleNamespace(**kwargs)
def dispatch(self, *args, **kwargs):
def dispatch(*args, **kwargs):
self = args[0] # If this fails, you forgot to do Event(stuff) first
self.stop_processing = False
self.prevent_default = False
for item in list(EVENT_CALLBACKS[self.name]):
item[1](self, *args, **kwargs)
item[1](*args, **kwargs)
if self.stop_processing:
break