avoid adding the append callback twice if the same source is added twice

This commit is contained in:
2021-06-04 14:48:28 +02:00
parent 61b068037b
commit dcaef9506e

View File

@@ -7,28 +7,26 @@ class Cacher:
def __init__(self):
self.caches = {}
def add_source(self, src, size=100):
name = src.name
if not src.is_connected:
raise RuntimeError(f"source NOT connected: {name}")
cache = self.add_cache(name, size=size)
src.add_callback(cache.callback_append)
return cache
def add_cache(self, name, size=100):
# key contains size in case multiple caches per name with different sizes are needed
key = (name, size)
caches = self.caches
try:
return caches[key]
return caches[name]
except KeyError:
caches[key] = cache = Cache(size=size)
caches[name] = cache = Cache(size=size)
src.add_callback(cache.callback_append)
return cache
def __repr__(self):
return repr(self.caches)
class Cache:
def __init__(self, size=100):