Got rid of some unnecessary overhead with creating lambdas for the adapter functions

This commit is contained in:
Sumner Evans
2020-05-16 23:47:45 -06:00
parent 00516581dc
commit 3e0cd6f94c
7 changed files with 45 additions and 31 deletions

View File

@@ -3,11 +3,11 @@ v0.9.3
* **Features**
* The Albums tab is now paginated.
* The Albums tab is now paginated with configurable page sizes.
* You can sort the Albums tab ascending or descending.
* The amount of the song that is cached is now shown while streaming a song.
* The dialog for resuming a play queue from another device has been greatly
improved and is now less intrusive.
* The notification for resuming a play queue is now a non-modal
notification that pops up right above the player controls.
* This release has a ton of under-the-hood changes to make things more robust
and performant.

View File

@@ -887,7 +887,7 @@ class SublimeMusicApp(Gtk.Application):
def update_play_state_from_server(self, prompt_confirm: bool = False):
# TODO (#129): need to make the play queue list loading for the duration here if
# prompt_confirm is False.
if (was_playing := self.app_config.state.playing) :
if not prompt_confirm and self.app_config.state.playing:
assert self.player
self.player.pause()
self.app_config.state.playing = False
@@ -933,6 +933,9 @@ class SublimeMusicApp(Gtk.Application):
resume_text += "?"
def on_resume_click():
if was_playing := self.app_config.state.playing:
self.on_play_pause()
self.app_config.state.play_queue = new_play_queue
self.app_config.state.song_progress = play_queue.position
self.app_config.state.current_song_index = (

View File

@@ -218,10 +218,12 @@ class AlbumsPanel(Gtk.Box):
self.updating_query = False
try:
# Never force. We invalidate the cache ourselves (force is used when sort
# params change). TODO I don't think that is the case now probaly can just
# force=force here
genres_future = AdapterManager.get_genres(force=False)
force = force and (
app_config is not None
and (state := app_config.state) is not None
and state.current_album_search_query.type == AlbumSearchQuery.Type.GENRE
)
genres_future = AdapterManager.get_genres(force=force)
genres_future.add_done_callback(lambda f: GLib.idle_add(get_genres_done, f))
except Exception:
self.updating_query = False

View File

@@ -86,14 +86,14 @@ class AlbumWithSongs(Gtk.Box):
self.play_next_btn = IconButton(
"go-top-symbolic",
"Play all of the songs in this album next",
action_name="app.play-next",
sensitive=False,
)
album_title_and_buttons.pack_start(self.play_next_btn, False, False, 5)
self.add_to_queue_btn = IconButton(
"go-jump-symbolic",
"Add all the songs in this album to the end of the play queue",
action_name="app.add-to-queue",
sensitive=False,
)
album_title_and_buttons.pack_start(self.add_to_queue_btn, False, False, 5)
@@ -276,9 +276,12 @@ class AlbumWithSongs(Gtk.Box):
self.play_btn.set_sensitive(True)
self.shuffle_btn.set_sensitive(True)
self.download_all_btn.set_sensitive(AdapterManager.can_batch_download_songs())
self.play_next_btn.set_action_target_value(GLib.Variant("as", song_ids))
self.add_to_queue_btn.set_action_target_value(GLib.Variant("as", song_ids))
self.download_all_btn.set_sensitive(AdapterManager.can_batch_download_songs())
self.play_next_btn.set_action_name("app.add-to-queue")
self.add_to_queue_btn.set_action_name("app.play-next")
util.diff_song_store(self.album_song_store, new_store)
self.loading_indicator.hide()

View File

@@ -91,7 +91,8 @@ class MainWindow(Gtk.ApplicationWindow):
def update(self, app_config: AppConfiguration, force: bool = False):
notification = app_config.state.current_notification
if notification and hash(notification) != self.current_notification_hash:
if notification and (h := hash(notification)) != self.current_notification_hash:
self.current_notification_hash = h
self.notification_text.set_markup(notification.markup)
for c in self.notification_actions.get_children():

View File

@@ -171,7 +171,7 @@ class PlaylistList(Gtk.Box):
self.update_list(**kwargs)
@util.async_callback(
lambda *a, **k: AdapterManager.get_playlists(*a, **k),
AdapterManager.get_playlists,
before_download=lambda self: self.loading_indicator.show_all(),
on_failure=lambda self, e: self.loading_indicator.hide(),
)
@@ -437,7 +437,7 @@ class PlaylistDetailPanel(Gtk.Overlay):
)
@util.async_callback(
lambda *a, **k: AdapterManager.get_playlist_details(*a, **k),
AdapterManager.get_playlist_details,
before_download=lambda self: self.show_loading_all(),
on_failure=lambda self, e: self.playlist_view_loading_box.hide(),
)
@@ -498,7 +498,7 @@ class PlaylistDetailPanel(Gtk.Overlay):
self.play_shuffle_buttons.show_all()
@util.async_callback(
lambda *a, **k: AdapterManager.get_cover_art_filename(*a, **k),
AdapterManager.get_cover_art_filename,
before_download=lambda self: self.playlist_artwork.set_loading(True),
on_failure=lambda self, e: self.playlist_artwork.set_loading(False),
)
@@ -711,7 +711,7 @@ class PlaylistDetailPanel(Gtk.Overlay):
def make_label(self, text: str = None, name: str = None, **params,) -> Gtk.Label:
return Gtk.Label(label=text, name=name, halign=Gtk.Align.START, **params,)
@util.async_callback(lambda *a, **k: AdapterManager.get_playlist_details(*a, **k))
@util.async_callback(AdapterManager.get_playlist_details)
def _update_playlist_order(
self, playlist: PlaylistDetails, app_config: AppConfiguration, **kwargs,
):

View File

@@ -347,10 +347,9 @@ def async_callback(
"""
Defines the ``async_callback`` decorator.
When a function is annotated with this decorator, the function becomes the
done callback for the given future-generating lambda function. The
annotated function will be called with the result of the future generated
by said lambda function.
When a function is annotated with this decorator, the function becomes the done
callback for the given result-generating lambda function. The annotated function
will be called with the result of the Result generated by said lambda function.
:param future_fn: a function which generates an :class:`AdapterManager.Result`.
"""
@@ -369,7 +368,7 @@ def async_callback(
if before_download:
GLib.idle_add(before_download, self)
def future_callback(f: Result):
def future_callback(is_immediate: bool, f: Result):
try:
result = f.result()
except Exception as e:
@@ -377,20 +376,26 @@ def async_callback(
GLib.idle_add(on_failure, self, e)
return
GLib.idle_add(
lambda: callback_fn(
self,
result,
app_config=app_config,
force=force,
order_token=order_token,
)
fn = functools.partial(
callback_fn,
self,
result,
app_config=app_config,
force=force,
order_token=order_token,
)
future: Result = future_fn(
if is_immediate:
GLib.idle_add(fn)
else:
fn()
result: Result = future_fn(
*args, before_download=on_before_download, force=force, **kwargs,
)
future.add_done_callback(future_callback)
result.add_done_callback(
functools.partial(future_callback, result.data_is_available)
)
return wrapper