unsubscribe is no more handled in untrack_pid

This commit is contained in:
Berenice Medel Sánchez 2021-12-10 10:38:05 -06:00 committed by Chris McCord
parent 0579f9f293
commit bdb4319f16
3 changed files with 9 additions and 18 deletions

View file

@ -19,7 +19,7 @@ defmodule Phoenix.Presence.Client do
end end
def untrack(topic, key) do def untrack(topic, key) do
GenServer.call(PresenceClient, {:untrack, self(), to_string(topic), key}) GenServer.call(PresenceClient, {:untrack, self(), topic, to_string(key)})
end end
def init(opts) do def init(opts) do
@ -55,11 +55,12 @@ defmodule Phoenix.Presence.Client do
end end
defp track_pid(state, pid, topic, key, meta) do defp track_pid(state, pid, topic, key, meta) do
#presences are handled when the presence_diff event received # presences are handled when the presence_diff event is received
case Map.fetch(state.topics, topic) do case Map.fetch(state.topics, topic) do
{:ok, _topic_content} -> {:ok, _topic_content} ->
state.presence_mod.track(pid, topic, key, meta) state.presence_mod.track(pid, topic, key, meta)
state state
:error -> :error ->
# subscribe to topic we weren't yet tracking # subscribe to topic we weren't yet tracking
Phoenix.PubSub.subscribe(state.pubsub, topic) Phoenix.PubSub.subscribe(state.pubsub, topic)
@ -69,18 +70,8 @@ defmodule Phoenix.Presence.Client do
end end
defp untrack_pid(state, pid, topic, key) do defp untrack_pid(state, pid, topic, key) do
state.presence_mod.untrack(pid, topic, key)
# remove presence from state.topics
if Map.has_key?(state.topics, topic) do if Map.has_key?(state.topics, topic) do
presences_count = topic_presences_count(state, topic) state.presence_mod.untrack(pid, topic, key)
# if no more presences for given topic, unsubscribe
if presences_count == 0 do
Phoenix.PubSub.unsubscribe(state.pubsub, topic)
update_topics_state(:remove_topic, state, topic, key)
else
update_topics_state(:remove_presence, state, topic, key)
end
else else
state state
end end
@ -116,7 +107,6 @@ defmodule Phoenix.Presence.Client do
end end
defp handle_leave({left_key, meta}, {state, topic}) do defp handle_leave({left_key, meta}, {state, topic}) do
left_meta = Map.get(meta, :metas, [])
updated_state = update_topics_state(:remove_presence, state, topic, left_key) updated_state = update_topics_state(:remove_presence, state, topic, left_key)
state.client.handle_leave(topic, left_key, meta, state) state.client.handle_leave(topic, left_key, meta, state)
{updated_state, topic} {updated_state, topic}

View file

@ -17,7 +17,7 @@ defmodule LiveBeats.PresenceClient do
{:ok, %{}} {:ok, %{}}
end end
def handle_join(topic, key, meta, state) do def handle_join(topic, key, _meta, state) do
active_users_topic = active_users_topic =
topic topic
|> profile_identifier() |> profile_identifier()
@ -28,7 +28,7 @@ defmodule LiveBeats.PresenceClient do
{:ok, state} {:ok, state}
end end
def handle_leave(topic, key, presence, state) do def handle_leave(topic, key, _meta, state) do
active_users_topic = active_users_topic =
topic topic
|> profile_identifier() |> profile_identifier()

View file

@ -159,7 +159,8 @@ defmodule LiveBeatsWeb.ProfileLive do
end end
def handle_info({LiveBeats.PresenceClient, %{user_left: user_id}}, socket) do def handle_info({LiveBeats.PresenceClient, %{user_left: user_id}}, socket) do
updated_presences = socket.assigns.presences |> Enum.reject(fn user -> user.id == user_id end) updated_presences = socket.assigns.presences
|> Enum.reject(fn user -> user.id == String.to_integer(user_id) end)
{:noreply, assign(socket, :presences, updated_presences)} {:noreply, assign(socket, :presences, updated_presences)}
end end