2022-05-10 20:34:29 +01:00
|
|
|
class Subject:
|
|
|
|
def __init__(self):
|
|
|
|
"""list of current observers"""
|
|
|
|
|
|
|
|
self._observables = []
|
|
|
|
|
|
|
|
def notify(self, modifier=None):
|
2022-05-14 14:05:48 +01:00
|
|
|
"""run callbacks on update"""
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
for observer in self._observables:
|
2022-05-14 14:05:48 +01:00
|
|
|
observer.on_update()
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
def add(self, observer):
|
|
|
|
"""adds an observer to observables"""
|
|
|
|
|
|
|
|
if observer not in self._observables:
|
|
|
|
self._observables.append(observer)
|
|
|
|
|
|
|
|
def remove(self, observer):
|
|
|
|
"""removes an observer from observables"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._observables.remove(observer)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get(self) -> list:
|
|
|
|
"""returns the current observables"""
|
2022-05-14 14:05:48 +01:00
|
|
|
|
2022-05-10 20:34:29 +01:00
|
|
|
return self._observables
|
|
|
|
|
|
|
|
def clear(self):
|
2022-05-14 14:05:48 +01:00
|
|
|
"""clears the observables list"""
|
|
|
|
|
2022-05-10 20:34:29 +01:00
|
|
|
self._observables.clear()
|