2020-03-29 14:37:30 +02:00
|
|
|
__callbacks = {}
|
2020-02-16 14:27:17 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-16 14:27:17 +01:00
|
|
|
def register(event, callback, *args, **kwargs):
|
2020-04-04 08:44:41 +02:00
|
|
|
cb = callback
|
2020-06-01 11:35:26 +02:00
|
|
|
if args or kwargs:
|
2020-04-04 08:44:41 +02:00
|
|
|
cb = lambda: callback(*args, **kwargs)
|
|
|
|
|
|
|
|
__callbacks.setdefault(event, []).append(cb)
|
2020-02-16 14:27:17 +01:00
|
|
|
|
2021-03-13 14:09:42 +01:00
|
|
|
def register_exclusive(event, callback, *args, **kwargs):
|
|
|
|
cb = callback
|
|
|
|
if args or kwargs:
|
|
|
|
cb = lambda: callback(*args, **kwargs)
|
|
|
|
|
|
|
|
__callbacks[event] = [cb]
|
|
|
|
|
2020-06-29 07:44:22 +02:00
|
|
|
def unregister(event):
|
|
|
|
if event in __callbacks:
|
|
|
|
del __callbacks[event]
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-22 13:42:44 +01:00
|
|
|
def clear():
|
2020-03-29 14:37:30 +02:00
|
|
|
__callbacks.clear()
|
2020-02-22 13:42:44 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-04 08:44:41 +02:00
|
|
|
def trigger(event, *args, **kwargs):
|
2020-05-02 13:54:45 +02:00
|
|
|
cb = __callbacks.get(event, [])
|
2020-05-03 11:15:52 +02:00
|
|
|
if len(cb) == 0:
|
|
|
|
return False
|
2020-05-02 13:54:45 +02:00
|
|
|
|
|
|
|
for callback in cb:
|
2020-06-01 11:35:26 +02:00
|
|
|
if args or kwargs:
|
2020-04-04 08:44:41 +02:00
|
|
|
callback(*args, **kwargs)
|
2020-06-01 11:35:26 +02:00
|
|
|
else:
|
|
|
|
callback()
|
2020-05-02 13:54:45 +02:00
|
|
|
return True
|
2020-02-16 14:27:17 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-16 14:27:17 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|