Skip to content

Commit

Permalink
Merge 9635ecd into 8cf18bc
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanepires committed Apr 12, 2020
2 parents 8cf18bc + 9635ecd commit de89707
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docutils==0.14
idna==2.7
imagesize==1.0.0
jinja2==2.10
markupsafe==1.0
markupsafe==1.1.1
more-itertools==4.2.0
packaging==17.1
pluggy==0.6.0
Expand Down
17 changes: 16 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
STRING_EVENT_ID = '42'
OBJECT_EVENT_ID = object()
NUMERIC_EVENT_ID = 42
STRING_PARAMETER = 'str'
INTEGER_PARAMETER = 42
OBJECT_PARAMETER = object()


class CustomEvent(Event):
Expand Down Expand Up @@ -51,11 +54,23 @@ def test_dispatcher_custom_event(EventType, event_id):
event = EventType()

dispatcher.add_listener(event_id, listener)
e = dispatcher.dispatch(event_id, event)
e = dispatcher.dispatch(event_id, event=event)

assert listener.call_count == 1
assert e == event

@pytest.mark.parametrize('event_id', [STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID])
@pytest.mark.parametrize('param_1', [STRING_PARAMETER, INTEGER_PARAMETER, OBJECT_PARAMETER])
@pytest.mark.parametrize('param_2', [STRING_PARAMETER, INTEGER_PARAMETER, OBJECT_PARAMETER])
def test_dispatcher_with_args(event_id, param_1, param_2):
dispatcher = EventDispatcher()
listener = mock.MagicMock()

dispatcher.add_listener(event_id, listener)
e = dispatcher.dispatch(event_id, param_1, param_2)

assert listener.call_count == 1
assert (listener.call_args) == ((e, param_1, param_2),)

def test_propagation():
dispatcher = EventDispatcher()
Expand Down
8 changes: 4 additions & 4 deletions whistle/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self):
self._listeners = {}
self._sorted = {}

def dispatch(self, event_id, event=None):
def dispatch(self, event_id, *args, event=None):
if event is None:
event = Event()

Expand All @@ -24,7 +24,7 @@ def dispatch(self, event_id, event=None):
if not event_id in self._listeners:
return event

self.do_dispatch(self.get_listeners(event_id), event)
self.do_dispatch(self.get_listeners(event_id), event, *args)

return event

Expand Down Expand Up @@ -94,9 +94,9 @@ def remove_listener(self, event_id, listener):
if event_id in self._sorted:
del self._sorted[event_id]

def do_dispatch(self, listeners, event):
def do_dispatch(self, listeners, event, *args):
for listener in listeners:
listener(event)
listener(event, *args)
if event.propagation_stopped: break

def sort_listeners(self, event_id):
Expand Down

0 comments on commit de89707

Please sign in to comment.