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