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
def untrack(topic, key) do
GenServer.call(PresenceClient, {:untrack, self(), to_string(topic), key})
GenServer.call(PresenceClient, {:untrack, self(), topic, to_string(key)})
end
def init(opts) do
@ -55,11 +55,12 @@ defmodule Phoenix.Presence.Client do
end
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
{:ok, _topic_content} ->
state.presence_mod.track(pid, topic, key, meta)
state
:error ->
# subscribe to topic we weren't yet tracking
Phoenix.PubSub.subscribe(state.pubsub, topic)
@ -69,25 +70,15 @@ defmodule Phoenix.Presence.Client do
end
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
presences_count = topic_presences_count(state, topic)
# 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
state.presence_mod.untrack(pid, topic, key)
else
state
end
end
defp merge_diff(state, topic, %{leaves: leaves, joins: joins}) do
#add new topic if needed
# add new topic if needed
updated_state =
if Map.has_key?(state.topics, topic) do
state
@ -116,7 +107,6 @@ defmodule Phoenix.Presence.Client do
end
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)
state.client.handle_leave(topic, left_key, meta, state)
{updated_state, topic}

View file

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

View file

@ -159,7 +159,8 @@ defmodule LiveBeatsWeb.ProfileLive do
end
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)}
end