Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Interface dispatchers on_xyz_event no longer need names or ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Tervala authored and Justin Tervala committed Feb 2, 2018
1 parent 7365787 commit 493f7eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,8 +2,17 @@
<!-- Use the tags Added, Changed, Deprecated, Removed, Fixed, Security, and
Contributor to describe changes -->

## [0.6.6]
###### 2018-02-02

### Changed
* Omitting `sender_uids` or `names` on `dispatcher.on_xyz_event` decorators
in interfaces now registers the decorated function for all senders. This
is consistent with the previously inaccurate code examples in the tutorials.


## [0.6.5]
###### 2018-02-01
###### 2018-02-02

### Added
* Webpack is now used to increase UI performance
Expand Down
10 changes: 7 additions & 3 deletions interfaces/dispatchers.py
Expand Up @@ -306,7 +306,7 @@ def register_events(self, func, events, sender_uids=None, names=None, weak=True)
names = convert_to_iterable(names)
entry_ids = set(sender_uids) | set(names)
if not entry_ids:
raise ValueError('Either sender_uid or name must specified')
entry_ids = ['all']
for entry_id in entry_ids:
self.__register_entry(entry_id, events, func, weak)

Expand Down Expand Up @@ -359,14 +359,18 @@ def _get_callbacks(self, sender_uid, sender_name, event):
set(func): The callbacks registered
"""
all_callbacks = set()
for sender_id in (sender_uid, sender_name):
for sender_id in ('all', sender_uid, sender_name):
if self.__is_event_registered_to_sender(sender_id, event):
all_callbacks |= set(self._router[sender_id][event])

return all_callbacks

def __is_event_registered_to_sender(self, sender_id, event):
return sender_id is not None and sender_id in self._router and event in self._router[sender_id]
return (sender_id is not None
and sender_id in self._router and event in self._router[sender_id])

def __is_all_registered_to_sender(self, event):
return 'all' in self._router and event in self._router['all']

def is_registered(self, entry, event, func):
"""Is a function registered for a given entry ID and event?
Expand Down
22 changes: 20 additions & 2 deletions tests/test_event_dispatcher.py
Expand Up @@ -29,8 +29,8 @@ def test_init(self):
self.assertDictEqual(self.router._router, {})

def test_register_events_no_uids_or_names(self):
with self.assertRaises(ValueError):
self.router.register_events(func, {WalkoffEvent.ActionStarted})
self.router.register_events(func, {WalkoffEvent.ActionStarted})
self.assert_router_structure({WalkoffEvent.ActionStarted}, uids={'all'})

def test_register_events_single_event_enum_single_uid(self):
self.router.register_events(func, {WalkoffEvent.ActionStarted}, sender_uids='a')
Expand Down Expand Up @@ -169,6 +169,24 @@ def y(data):
self.assertDictEqual(result, {'x': {'sender_uid': 'a', 'sender_name': 'b'}, 'count': 1})
self.assertTrue(result2['x'])

def test_dispatch_registed_no_sender_name_or_uid(self):
result = {'x': True, 'count': 0}
result2 = {'x': False}

def x(data):
result['x'] = data
result['count'] += 1

def y(data):
result2['x'] = True

self.router.register_events(x, {WalkoffEvent.ActionStarted})
self.router.register_events(y, {WalkoffEvent.ActionStarted})

self.router.dispatch(WalkoffEvent.ActionStarted, {'sender_uid': 'a', 'sender_name': 'b'})
self.assertDictEqual(result, {'x': {'sender_uid': 'a', 'sender_name': 'b'}, 'count': 1})
self.assertTrue(result2['x'])

def test_dispatch_controller_event(self):
result = {'x': False}

Expand Down

0 comments on commit 493f7eb

Please sign in to comment.