-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.py
33 lines (28 loc) · 943 Bytes
/
event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Event:
def __init__(self):
# Initialise a list of listeners
self.__listeners = []
# Define a getter for the 'on' property which returns the decorator.
@property
def on(self):
# A decorator to run addListener on the input function.
def wrapper(func):
self.add_listener(func)
return func
return wrapper
# Add and remove functions from the list of listeners.
def add_listener(self, func):
if func in self.__listeners:
return
self.__listeners.append(func)
def remove_listener(self, func):
if func not in self.__listeners:
return
self.__listeners.remove(func)
# Trigger events.
def trigger(self, *args, **kwargs):
# Run all the functions that are saved.
if args is None:
args = []
for func in self.__listeners:
func(*args, **kwargs)