-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservable.py
81 lines (65 loc) · 1.99 KB
/
observable.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
observable.py outlines the abstract and concrete classes for a Subject/Observable
object.
"""
from abc import ABC, abstractmethod
from api_constants import *
from updater import Updater
class AbstractObservable(ABC):
"""
Abstract Observable is the abstract superclass for an observed object, whose state change
calls for an update call for the subscriber of observer objects observing this (its child)
subject.
"""
@abstractmethod
def attach(self, observer):
"""
Attaches an observer to the subject
:param observer: the observer to attach to the subject
:return: None
"""
pass
@abstractmethod
def detach(self, observer):
"""
detaches an observer from the subject
:param observer: the observer to detach
:return: None
"""
pass
@abstractmethod
def notify(self):
"""
Notify all subscribed observers about some event
:return: None
"""
pass
class Observable(AbstractObservable):
"""
A concrete implementation of the AbstractObservable,
This subject monitors any changes from the local copy of subscribed bids with the version
that is stored on the user's data on the server.
"""
_state = None
_observers = []
subscribed_bids = []
def attach(self, observer):
self._observers.append(observer)
def detach(self, observer):
self._observers.remove(observer)
def notify(self):
for obs in self._observers:
obs.update()
def start_listening(self):
Updater(1, self.check_for_changes)
def check_for_changes(self, user):
if not user:
return
out = []
for b in api_get(bid_url):
if b["id"] in user.subscribed_bids:
out.append(b)
compare = [x for x in out + self.subscribed_bids if x not in out or x not in self.subscribed_bids]
if not compare:
return
self.notify()