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):
|
2022-07-16 22:11:28 +01:00
|
|
|
"""adds an observer to _observers"""
|
2022-05-10 20:34:29 +01:00
|
|
|
|
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):
|
2022-07-16 22:11:28 +01:00
|
|
|
"""removes an observer from _observers"""
|
2022-05-10 20:34:29 +01:00
|
|
|
|
2022-08-02 10:06:35 +01:00
|
|
|
if observer in self._observers:
|
2022-06-20 00:09:27 +01:00
|
|
|
self._observers.remove(observer)
|
2022-05-10 20:34:29 +01:00
|
|
|
|
|
|
|
def get(self) -> list:
|
2022-07-16 22:11:28 +01:00
|
|
|
"""returns the current _observers"""
|
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-07-16 22:11:28 +01:00
|
|
|
"""clears the _observers list"""
|
2022-05-14 14:05:48 +01:00
|
|
|
|
2022-06-20 00:09:27 +01:00
|
|
|
self._observers.clear()
|