Use streams for scrobbled tracks
This commit is contained in:
+10
-2
@@ -17,11 +17,19 @@ defmodule LastFm.Feed do
|
||||
:ets.delete_all_objects(__MODULE__)
|
||||
:ets.insert(__MODULE__, data)
|
||||
|
||||
Phoenix.PubSub.broadcast(LastFm.PubSub, "feed:update", %{tracks: data})
|
||||
Phoenix.PubSub.broadcast(LastFm.PubSub, "feed:update", %{tracks: tracks})
|
||||
end
|
||||
|
||||
def all do
|
||||
:ets.tab2list(__MODULE__)
|
||||
m = [
|
||||
{
|
||||
{:_, :_},
|
||||
[],
|
||||
[{:element, 2, :"$_"}]
|
||||
}
|
||||
]
|
||||
|
||||
:ets.select(__MODULE__, m)
|
||||
|> Enum.reverse()
|
||||
end
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> stream_configure(:recent_tracks,
|
||||
dom_id: fn track -> "track-#{track.scrobbled_at_uts}" end
|
||||
)
|
||||
|> stream(:recent_tracks, recent_tracks)
|
||||
|> assign(
|
||||
page_title: gettext("Stats"),
|
||||
collection_count_by_format: collection_count_by_format,
|
||||
@@ -33,20 +37,12 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
||||
collection_count: collection_count,
|
||||
wishlist_count: wishlist_count,
|
||||
latest_record: latest_record,
|
||||
recent_tracks: recent_tracks,
|
||||
nav_section: :stats
|
||||
)}
|
||||
end
|
||||
|
||||
def handle_info(%{tracks: tracks}, socket) do
|
||||
socket =
|
||||
if socket.assigns.recent_tracks !== tracks do
|
||||
assign(socket, :recent_tracks, tracks)
|
||||
else
|
||||
socket
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
{:noreply, stream(socket, :recent_tracks, tracks, reset: true)}
|
||||
end
|
||||
|
||||
# The Tailwind build step requires all needed classes to be explicitly referenced
|
||||
|
||||
@@ -65,8 +65,8 @@
|
||||
<h1 class="mt-5 text-base lg:text-2xl text-gray-900 dark:text-gray-200 font-semibold">
|
||||
<%= gettext("Scrobble activity") %>
|
||||
</h1>
|
||||
<ul role="list" class="-mb-8 mt-5">
|
||||
<li :for={{id, track} <- @recent_tracks} id={"track-#{id}"} class="group">
|
||||
<ul id="scrobble-activity" role="list" class="-mb-8 mt-5" phx-update="stream">
|
||||
<li :for={{id, track} <- @streams.recent_tracks} id={"track-#{id}"} class="group">
|
||||
<div class="relative pb-8">
|
||||
<span
|
||||
class="group-last:hidden absolute left-6 top-6 -ml-px h-full w-0.5 bg-gray-200"
|
||||
|
||||
@@ -269,7 +269,7 @@ msgid "Something went wrong!"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/components/layouts/app.html.heex:7
|
||||
#: lib/music_library_web/live/stats_live/index.ex:30
|
||||
#: lib/music_library_web/live/stats_live/index.ex:34
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Stats"
|
||||
msgstr ""
|
||||
|
||||
Reference in New Issue
Block a user