2022-05-10 20:34:29 +01:00
|
|
|
class Subject:
|
2022-06-20 00:09:27 +01:00
|
|
|
"""Adds support for observers"""
|
|
|
|
|
2022-05-10 20:34:29 +01:00
|
|
|
def __init__(self):
|
|
|
|
"""list of current observers"""
|
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
self._observers = list()
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
[o.on_update(modifier) for o in self._observers]
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
def add(self, observer):
|
|
|
|
"""adds an observer to observables"""
|
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
if observer not in self._observers:
|
|
|
|
self._observers.append(observer)
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
def remove(self, observer):
|
|
|
|
"""removes an observer from observables"""
|
|
|
|
|
|
|
|
try:
|
2022-06-20 00:09:27 +01:00
|
|
|
self._observers.remove(observer)
|
2022-05-10 20:34:29 +01:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get(self) -> list:
|
|
|
|
"""returns the current observables"""
|
2022-05-14 14:05:48 +01:00
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
return self._observers
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
def clear(self):
|
2022-05-14 14:05:48 +01:00
|
|
|
"""clears the observables list"""
|
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
self._observers.clear()
|