Skip to content

Commit

Permalink
Extend tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Jun 23, 2022
1 parent 646b5a3 commit ddd1114
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pymitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -162,22 +164,27 @@ 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)

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:
Expand Down
44 changes: 44 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit ddd1114

Please sign in to comment.