X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/687353c5fefbeea8486cbb28dedf52a53dc65e9b..194615a88fac0c4259b3c2217c8e13372b7c3b86:/lib/python/Components/Element.py diff --git a/lib/python/Components/Element.py b/lib/python/Components/Element.py index fc709139..437d934b 100644 --- a/lib/python/Components/Element.py +++ b/lib/python/Components/Element.py @@ -4,12 +4,30 @@ 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. + CHANGED_SPECIFIC = 3 # second tuple will specify what exactly changed + CHANGED_POLL = 4 # a timer expired + def __init__(self): self.downstream_elements = CList() - self.upstream_elements = CList() self.master = None self.source = None + self.__suspended = True + self.cache = None def connectDownstream(self, downstream): self.downstream_elements.append(downstream) @@ -17,9 +35,9 @@ class Element: self.master = downstream def connectUpstream(self, upstream): - self.upstream_elements.append(upstream) - self.source = upstream # for single-source elements (i.e., most of them.) - self.changed() + assert self.source is None + self.source = upstream + self.changed((self.CHANGED_DEFAULT,)) def connect(self, upstream): self.connectUpstream(upstream) @@ -30,11 +48,13 @@ class Element: # we should not disconnect from upstream if # there are still elements depending on us. assert len(self.downstream_elements) == 0, "there are still downstream elements left" - - # disconnect all upstream elements from us - for upstream in self.upstream_elements: - upstream.disconnectDownstream(self) - + + # Sources don't have a source themselves. don't do anything here. + if self.source is not None: + self.source.disconnectDownstream(self) + # sources are owned by the Screen, so don't destroy them here. + self.destroy() + def disconnectDownstream(self, downstream): self.downstream_elements.remove(downstream) if self.master == downstream: @@ -44,5 +64,33 @@ class Element: self.disconnectAll() # default action: push downstream - def changed(self): - self.downstream_elements.changed() + def changed(self, *args, **kwargs): + self.cache = { } + self.downstream_elements.changed(*args, **kwargs) + self.cache = None + + def reconnectUpstream(self, new_upstream): + assert self.source is not None + self.source = new_upstream + + 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 + + def destroy(self): + pass