small change
[enigma2.git] / lib / python / Components / Element.py
index 122184661b3435096b599147429a8d481f3301b8..baab4cfb46d30284be8d3836d61e346e49a8b5bf 100644 (file)
@@ -4,7 +4,18 @@ from Tools.CList import CList
 # Render Converter Converter Source
 
 # a bidirectional connection
-class Element:
+
+def cached(f):
+       name = f.__name__
+       def wrapper(self):
+               if self.cache is None:
+                       return f(self)
+               if name not in self.cache:
+                       self.cache[name] = (True, f(self))
+               return self.cache[name][1]
+       return wrapper
+
+class Element(object):
        CHANGED_DEFAULT = 0   # initial "pull" state
        CHANGED_ALL = 1       # really everything changed
        CHANGED_CLEAR = 2     # we're expecting a real update soon. don't bother polling NOW, but clear data.
@@ -15,7 +26,8 @@ class Element:
                self.downstream_elements = CList()
                self.master = None
                self.source = None
-               self.clearCache()
+               self.__suspended = True
+               self.cache = None
 
        def connectDownstream(self, downstream):
                self.downstream_elements.append(downstream)
@@ -51,13 +63,29 @@ class Element:
 
        # default action: push downstream
        def changed(self, *args, **kwargs):
-               self.clearCache()
+               self.cache = { }
                self.downstream_elements.changed(*args, **kwargs)
-               self.clearCache()
+               self.cache = None
 
        def reconnectUpstream(self, new_upstream):
                assert self.source is not None
                self.source = new_upstream
 
-       def clearCache(self):
-               self.cache = None
+       def setSuspend(self, suspended):
+               changed = self.__suspended != suspended
+               if not self.__suspended and suspended:
+                       self.doSuspend(1)
+               elif self.__suspended and not suspended:
+                       self.doSuspend(0)
+                       
+               self.__suspended = suspended
+               if self.source is not None and changed:
+                       self.source.checkSuspend()
+       
+       suspended = property(lambda self: self.__suspended, setSuspend)
+       
+       def checkSuspend(self):
+               self.suspended = reduce(lambda x, y: x and y.__suspended, self.downstream_elements, True)
+
+       def doSuspend(self, suspend):
+               pass