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:
@@ -97,6 +97,20 @@ defmodule MusicLibraryWeb.LiveHelpers.RecordActions do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Manages PubSub subscriptions when navigating between records.
|
||||
Unsubscribes from the previous record (if any) and subscribes to the new one,
|
||||
but only when the socket is connected.
|
||||
"""
|
||||
def manage_subscription(socket, new_id) do
|
||||
if Phoenix.LiveView.connected?(socket) do
|
||||
if socket.assigns[:record], do: Records.unsubscribe(socket.assigns.record.id)
|
||||
Records.subscribe(new_id)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
def handle_chats_changed(socket) do
|
||||
{:noreply,
|
||||
assign(socket, :chat_count, Chats.count_chats(:record, socket.assigns.record.musicbrainz_id))}
|
||||
|
||||
Reference in New Issue
Block a user