some minor speedups using caches and more selective updating
[enigma2.git] / lib / python / Components / Element.py
1 from Tools.CList import CList
2
3 # down                       up
4 # Render Converter Converter Source
5
6 # a bidirectional connection
7 class Element:
8         CHANGED_DEFAULT = 0   # initial "pull" state
9         CHANGED_ALL = 1       # really everything changed
10         CHANGED_CLEAR = 2     # we're expecting a real update soon. don't bother polling NOW, but clear data.
11         CHANGED_SPECIFIC = 3  # second tuple will specify what exactly changed
12         CHANGED_POLL = 4      # a timer expired
13
14         def __init__(self):
15                 self.downstream_elements = CList()
16                 self.master = None
17                 self.source = None
18                 self.clearCache()
19
20         def connectDownstream(self, downstream):
21                 self.downstream_elements.append(downstream)
22                 if self.master is None:
23                         self.master = downstream
24         
25         def connectUpstream(self, upstream):
26                 assert self.source is None
27                 self.source = upstream
28                 self.changed((self.CHANGED_DEFAULT,))
29         
30         def connect(self, upstream):
31                 self.connectUpstream(upstream)
32                 upstream.connectDownstream(self)
33
34         # we disconnect from down to up
35         def disconnectAll(self):
36                 # we should not disconnect from upstream if
37                 # there are still elements depending on us.
38                 assert len(self.downstream_elements) == 0, "there are still downstream elements left"
39                 
40                 # Sources don't have a source themselves. don't do anything here.
41                 if self.source is not None:
42                         self.source.disconnectDownstream(self)
43         
44         def disconnectDownstream(self, downstream):
45                 self.downstream_elements.remove(downstream)
46                 if self.master == downstream:
47                         self.master = None
48                 
49                 if len(self.downstream_elements) == 0:
50                         self.disconnectAll()
51
52         # default action: push downstream
53         def changed(self, *args, **kwargs):
54                 self.clearCache()
55                 self.downstream_elements.changed(*args, **kwargs)
56                 self.clearCache()
57
58         def reconnectUpstream(self, new_upstream):
59                 assert self.source is not None
60                 self.source = new_upstream
61
62         def clearCache(self):
63                 self.cache = None