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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user