26 lines
801 B
Python
26 lines
801 B
Python
class Event():
|
|
def __init__(self):
|
|
self.__listeners = []
|
|
|
|
@property
|
|
def on(self, event, callback):
|
|
self.__listeners.append({event: callback})
|
|
|
|
def emit(self, event, data):
|
|
for listener in self.__listeners:
|
|
if event in listener:
|
|
# is this a awaitable?
|
|
if hasattr(listener[event], "__await__"):
|
|
listener[event](data)
|
|
else:
|
|
listener[event](data)
|
|
|
|
def addListener(self, event, callback):
|
|
if callback in self.__listeners:
|
|
return
|
|
self.__listeners.append({event: callback})
|
|
|
|
def removeListener(self, event, callback):
|
|
if callback not in self.__listeners:
|
|
return
|
|
self.__listeners.remove({event: callback}) |