add callback unit tests.

callback deregister now accepts iterable.
This commit is contained in:
onyx-and-iris
2022-07-27 20:49:45 +01:00
parent 95b1cb27da
commit f5c2293dce
4 changed files with 70 additions and 7 deletions

View File

@@ -42,13 +42,17 @@ class Callback:
if fns not in self._callbacks:
self._callbacks.append(fns)
def deregister(self, callback):
def deregister(self, fns: Union[Iterable, Callable]):
"""deregisters a callback from _callbacks"""
try:
self._callbacks.remove(callback)
except ValueError:
print(f"Failed to remove: {callback}")
iterator = iter(fns)
for fn in iterator:
if fn in self._callbacks:
self._callbacks.remove(fn)
except TypeError as e:
if fns in self._callbacks:
self._callbacks.remove(fns)
def clear(self):
"""clears the _callbacks list"""

View File

@@ -41,8 +41,9 @@ class EventClient(object):
self.base_client = ObsClient(**kwargs)
self.base_client.authenticate()
self.callback = Callback()
self.subscribe()
self.running = True
def subscribe(self):
worker = Thread(target=self.trigger, daemon=True)
worker.start()
@@ -52,6 +53,7 @@ class EventClient(object):
Triggers a callback on event received.
"""
self.running = True
while self.running:
self.data = json.loads(self.base_client.ws.recv())
event, data = (self.data["d"].get("eventType"), self.data["d"])