core: Add CoreListener.on_event()
The `on_event()` method is called on all events. By default, it forwards the event to the specific event handler methods. It's also a convenient method to override if you want to handle all events in one place.
This commit is contained in:
parent
90859c903b
commit
6238f55ae2
@ -19,7 +19,20 @@ class CoreListener(object):
|
||||
"""Helper to allow calling of core listener events"""
|
||||
listeners = pykka.ActorRegistry.get_by_class(CoreListener)
|
||||
for listener in listeners:
|
||||
getattr(listener.proxy(), event)(**kwargs)
|
||||
listener.proxy().on_event(event, **kwargs)
|
||||
|
||||
def on_event(self, event, **kwargs):
|
||||
"""
|
||||
Called on all events.
|
||||
|
||||
*MAY* be implemented by actor. By default, this method forwards the
|
||||
event to the specific event methods.
|
||||
|
||||
:param event: the event name
|
||||
:type event: string
|
||||
:param kwargs: any other arguments to the specific event handlers
|
||||
"""
|
||||
getattr(self, event)(**kwargs)
|
||||
|
||||
def track_playback_paused(self, track, time_position):
|
||||
"""
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import mock
|
||||
|
||||
from mopidy.core import CoreListener, PlaybackState
|
||||
from mopidy.models import Playlist, Track
|
||||
|
||||
@ -10,6 +12,15 @@ class CoreListenerTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.listener = CoreListener()
|
||||
|
||||
def test_on_event_forwards_to_specific_handler(self):
|
||||
self.listener.track_playback_paused = mock.Mock()
|
||||
|
||||
self.listener.on_event(
|
||||
'track_playback_paused', track=Track(), position=0)
|
||||
|
||||
self.listener.track_playback_paused.assert_called_with(
|
||||
track=Track(), position=0)
|
||||
|
||||
def test_listener_has_default_impl_for_track_playback_paused(self):
|
||||
self.listener.track_playback_paused(Track(), 0)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user