Skip to content

Commit

Permalink
feat: can declare listener as parameter too
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyguedess committed May 8, 2020
1 parent 8777793 commit 82080ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion onany/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def disthread(event: str, *args, **kwargs) -> Thread:
return thread


def listener(event: str) -> callable:
def listener(event: str, callback: callable = None) -> callable:
if callback:
return OnAny.listen(event, callback)

def wrapper(callback: callable) -> None:
return OnAny.listen(event, callback)

Expand Down
16 changes: 16 additions & 0 deletions tests/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from datetime import datetime


print_history = {}

def sprint(message: str) -> None:
print_history.update({
"last_print": message,
"history": print_history.get("history", []) + [{
"message": message,
"at": datetime.utcnow()
}]
})

def get_last_sprint() -> str:
return print_history.get("last_print")
11 changes: 11 additions & 0 deletions tests/test_onany.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Dict
from unittest import TestCase
from tests.generators import sprint, get_last_sprint
from onany import dispatch, disthread, listener


Expand All @@ -15,6 +16,16 @@ def test_dispatch_listen(self):

self.assertTrue(params.get("called_once"))

def test_dispatch_listen_registering_simple_callback(self):
listener(
"event.name",
lambda name:
sprint("Hello {}. Welcome to OnAny!".format(name)))

dispatch("event.name", "Clark Kent")

self.assertEqual(get_last_sprint(), "Hello Clark Kent. Welcome to OnAny!")

def test_thread_dispatch_listen(self):
callback = self.get_listener()
params = {
Expand Down

0 comments on commit 82080ef

Please sign in to comment.