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:
Claudio Ortolina
2026-05-07 10:14:10 +01:00
parent 365195ff75
commit 5f240fe573
4 changed files with 41 additions and 15 deletions
+8
View File
@@ -110,6 +110,14 @@ defmodule MusicLibrary.Records do
Phoenix.PubSub.subscribe(MusicLibrary.PubSub, "records:#{record_id}")
end
@doc """
Unsubscribes the calling process from updates for a given record.
"""
@spec unsubscribe(String.t()) :: :ok | {:error, term()}
def unsubscribe(record_id) do
Phoenix.PubSub.unsubscribe(MusicLibrary.PubSub, "records:#{record_id}")
end
@spec notify_update(Record.t()) :: :ok | {:error, term()}
def notify_update(record) do
Phoenix.PubSub.broadcast(