Fix stale PubSub subscriptions on record navigation
Subscription management was only done in mount/3, meaning
navigating to a different record via handle_params/3 left the
old subscription active. A background update for the old record
would then overwrite the currently displayed record.
Fix: manage subscriptions in handle_params/3 (unsubscribe old,
subscribe new) via shared RecordActions.manage_subscription/2,
and guard handle_info({:update,...}) against mismatched IDs.
This commit is contained in:
@@ -348,11 +348,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(%{"id" => record_id}, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Records.subscribe(record_id)
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:current_section, :collection)
|
||||
@@ -362,6 +358,8 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
RecordActions.manage_subscription(socket, id)
|
||||
|
||||
record = Records.get_record!(id)
|
||||
last_listened_track = ListeningStats.get_last_listened_track(record)
|
||||
play_count = ListeningStats.play_count(record)
|
||||
@@ -474,10 +472,14 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_info({:update, record}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> RecordActions.handle_record_updated(record)
|
||||
|> assign_similar_records()}
|
||||
if record.id == socket.assigns.record.id do
|
||||
{:noreply,
|
||||
socket
|
||||
|> RecordActions.handle_record_updated(record)
|
||||
|> assign_similar_records()}
|
||||
else
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
defp page_title(action, record) do
|
||||
|
||||
@@ -284,13 +284,9 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(%{"id" => record_id}, _session, socket) do
|
||||
def mount(_params, _session, socket) do
|
||||
current_date = DateTime.utc_now() |> DateTime.to_date()
|
||||
|
||||
if connected?(socket) do
|
||||
Records.subscribe(record_id)
|
||||
end
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(current_section: :wishlist)
|
||||
@@ -299,6 +295,8 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
RecordActions.manage_subscription(socket, id)
|
||||
|
||||
record = Records.get_record!(id)
|
||||
online_store_templates = OnlineStoreTemplates.list_enabled_templates()
|
||||
|
||||
@@ -367,7 +365,11 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_info({:update, record}, socket) do
|
||||
{:noreply, RecordActions.handle_record_updated(socket, record)}
|
||||
if record.id == socket.assigns.record.id do
|
||||
{:noreply, RecordActions.handle_record_updated(socket, record)}
|
||||
else
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
defp page_title(action, record) do
|
||||
|
||||
Reference in New Issue
Block a user