From ddd1114149d2e47ad46f764d13429dfc2f478f3a Mon Sep 17 00:00:00 2001 From: Marcel R Date: Thu, 23 Jun 2022 18:07:04 +0200 Subject: [PATCH] Extend tests. --- pymitter.py | 15 +++++++++++---- tests.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/pymitter.py b/pymitter.py index 77918f7..8f6b12e 100644 --- a/pymitter.py +++ b/pymitter.py @@ -77,6 +77,7 @@ def on(func): # create a new listener and add it self._event_tree.add_listener(event, Listener(func, event, ttl)) + if self.new_listener and event != self.new_listener_event: self.emit(self.new_listener_event, func, event) @@ -104,6 +105,7 @@ def on_any(func): # create a new listener and add it self._any_listeners.append(Listener(func, None, ttl)) + if self.new_listener: self.emit(self.new_listener_event, func) @@ -162,12 +164,12 @@ def listeners_all(self) -> List[Callable]: """ Returns all registered functions, ordered by their registration time. """ - listeners = [] - nodes = [self._event_tree] + listeners = list(self._any_listeners) + nodes = list(self._event_tree.nodes.values()) while nodes: node = nodes.pop(0) - listeners.extend(node.listeners) nodes.extend(node.nodes.values()) + listeners.extend(node.listeners) # sort them listeners = sorted(listeners, key=lambda listener: listener.time) @@ -175,9 +177,14 @@ def listeners_all(self) -> List[Callable]: return [listener.func for listener in listeners] def _emit(self, event: str, *args, **kwargs) -> List[Awaitable]: + listeners = self._event_tree.find_listeners(event) + if event != self.new_listener_event: + listeners.extend(self._any_listeners) + listeners = sorted(listeners, key=lambda listener: listener.time) + # call listeners in order, keep track of awaitables from coroutines functions awaitables = [] - for listener in self._event_tree.find_listeners(event): + for listener in listeners: # since listeners can emit events themselves, # deregister them before calling if needed if listener.ttl == 1: diff --git a/tests.py b/tests.py index a2dcfdd..8b49d4e 100644 --- a/tests.py +++ b/tests.py @@ -71,6 +71,50 @@ def handler(): ee.emit("on_all.foo") self.assertTrue(stack[-1] == "on_all") + def test_on_any(self): + ee = EventEmitter() + stack = [] + + @ee.on("foo") + def handler1(): + stack.append("foo") + + @ee.on_any() + def handler2(): + stack.append("bar") + + ee.emit("foo") + self.assertEqual(tuple(stack), ("foo", "bar")) + + def test_listeners(self): + ee = EventEmitter(wildcard=True) + + @ee.on("foo") + def h1(): + pass + + @ee.on("foo") + def h2(): + pass + + @ee.on("bar") + def h3(): + pass + + @ee.once("baz") + def h4(): + pass + + @ee.on_any + def h5(): + pass + + self.assertEqual(tuple(ee.listeners_any()), (h5,)) + self.assertEqual(tuple(ee.listeners_all()), (h1, h2, h3, h4, h5)) + self.assertEqual(tuple(ee.listeners("foo")), (h1, h2)) + self.assertEqual(tuple(ee.listeners("bar")), (h3,)) + self.assertEqual(tuple(ee.listeners("ba?")), (h3, h4)) + def test_emit_all(self): ee = EventEmitter(wildcard=True) stack = []