ML-168: broadcast index_changed event after background import
Import workers now broadcast :records_index_changed on "records:index_changed" after successful import via Records.broadcast_index_changed/0. CollectionLive.Index and WishlistLive.Index subscribe to the topic in mount/3 and reload their record streams on receipt, with a live_action guard to skip reloads when the grid is hidden behind a modal (:import, :barcode_scan). IndexActions.handle_index_changed/1 refreshes total_entries before reloading to keep the pagination bar accurate.
This commit is contained in:
@@ -126,4 +126,21 @@ defmodule MusicLibrary.Records do
|
||||
{:update, record}
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Broadcasts that the records index has changed (new record imported, deleted, etc.).
|
||||
Index LiveViews subscribe to this topic to auto-refresh.
|
||||
"""
|
||||
@spec broadcast_index_changed() :: :ok
|
||||
def broadcast_index_changed do
|
||||
Phoenix.PubSub.broadcast(MusicLibrary.PubSub, "records:index_changed", :records_index_changed)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Subscribes the calling process to records index change notifications.
|
||||
"""
|
||||
@spec subscribe_to_index() :: :ok | {:error, term()}
|
||||
def subscribe_to_index do
|
||||
Phoenix.PubSub.subscribe(MusicLibrary.PubSub, "records:index_changed")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ defmodule MusicLibrary.Worker.ImportFromMusicbrainzRelease do
|
||||
|
||||
use Oban.Worker, queue: :music_brainz, max_attempts: 3
|
||||
|
||||
alias MusicLibrary.Records
|
||||
alias MusicLibrary.Records.Record
|
||||
alias MusicLibrary.Worker.ErrorHandler
|
||||
|
||||
@@ -19,8 +20,12 @@ defmodule MusicLibrary.Worker.ImportFromMusicbrainzRelease do
|
||||
]
|
||||
|
||||
case MusicLibrary.Records.import_from_musicbrainz_release(release_id, opts) do
|
||||
{:ok, _record} -> :ok
|
||||
other -> ErrorHandler.to_oban_result(other)
|
||||
{:ok, _record} ->
|
||||
Records.broadcast_index_changed()
|
||||
:ok
|
||||
|
||||
other ->
|
||||
ErrorHandler.to_oban_result(other)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,8 +23,12 @@ defmodule MusicLibrary.Worker.ImportFromMusicbrainzReleaseGroup do
|
||||
]
|
||||
|
||||
case Records.import_from_musicbrainz_release_group(release_group_id, opts) do
|
||||
{:ok, _record} -> :ok
|
||||
other -> ErrorHandler.to_oban_result(other)
|
||||
{:ok, _record} ->
|
||||
Records.broadcast_index_changed()
|
||||
:ok
|
||||
|
||||
other ->
|
||||
ErrorHandler.to_oban_result(other)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
|
||||
|
||||
alias MusicLibrary.Chats
|
||||
alias MusicLibrary.Collection
|
||||
alias MusicLibrary.Records
|
||||
alias MusicLibraryWeb.Components.AddRecord
|
||||
alias MusicLibraryWeb.LiveHelpers.IndexActions
|
||||
|
||||
@@ -242,6 +243,10 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Records.subscribe_to_index()
|
||||
end
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:current_section, :collection)
|
||||
@@ -300,6 +305,15 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
|
||||
{:noreply, assign(socket, :chat_count, chat_count)}
|
||||
end
|
||||
|
||||
def handle_info(:records_index_changed, socket)
|
||||
when socket.assigns.live_action in [:index, :edit] do
|
||||
{:noreply, IndexActions.handle_index_changed(socket)}
|
||||
end
|
||||
|
||||
def handle_info(:records_index_changed, socket) do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_async(:collection_summary, {:ok, summary}, socket) do
|
||||
{:noreply, assign(socket, :collection_summary, summary)}
|
||||
|
||||
@@ -176,6 +176,10 @@ defmodule MusicLibraryWeb.WishlistLive.Index do
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Records.subscribe_to_index()
|
||||
end
|
||||
|
||||
current_date = Date.utc_today()
|
||||
|
||||
{:ok,
|
||||
@@ -217,6 +221,15 @@ defmodule MusicLibraryWeb.WishlistLive.Index do
|
||||
IndexActions.handle_cart_imported_async(socket, count)
|
||||
end
|
||||
|
||||
def handle_info(:records_index_changed, socket)
|
||||
when socket.assigns.live_action in [:index, :edit] do
|
||||
{:noreply, IndexActions.handle_index_changed(socket)}
|
||||
end
|
||||
|
||||
def handle_info(:records_index_changed, socket) do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
IndexActions.handle_delete(socket, id)
|
||||
|
||||
@@ -146,6 +146,18 @@ defmodule MusicLibraryWeb.LiveHelpers.IndexActions do
|
||||
{:noreply, load_and_assign_records(socket, socket.assigns.record_list_params)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles a PubSub notification that records have changed.
|
||||
Refreshes total_entries and reloads the record stream using the current parameters.
|
||||
"""
|
||||
def handle_index_changed(socket) do
|
||||
config = socket.assigns.index_config
|
||||
params = socket.assigns.record_list_params
|
||||
total_records = config.context_module.search_records_count(params.query)
|
||||
updated_params = %{params | total_entries: total_records}
|
||||
load_and_assign_records(socket, updated_params)
|
||||
end
|
||||
|
||||
defp record_page_title(record, config) do
|
||||
Enum.join(
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user