change key to string when tracking and untrackin presences

This commit is contained in:
Berenice Medel Sánchez 2021-12-09 11:56:02 -06:00 committed by Chris McCord
parent 463faafe71
commit c22e104a24
2 changed files with 13 additions and 8 deletions

View file

@ -15,11 +15,11 @@ defmodule Phoenix.Presence.Client do
end end
def track(topic, key, meta) do def track(topic, key, meta) do
GenServer.call(PresenceClient, {:track, self(), topic, key, meta}) GenServer.call(PresenceClient, {:track, self(), topic, to_string(key), meta})
end end
def untrack(topic, key) do def untrack(topic, key) do
GenServer.call(PresenceClient, {:untrack, self(), topic, key}) GenServer.call(PresenceClient, {:untrack, self(), to_string(topic), key})
end end
def init(opts) do def init(opts) do
@ -41,6 +41,11 @@ defmodule Phoenix.Presence.Client do
{:noreply, merge_diff(state, topic, diff)} {:noreply, merge_diff(state, topic, diff)}
end end
def handle_call(:state, _from, state) do
IO.inspect(state.topics, label: :state_topics)
{:reply, :ok, state}
end
def handle_call({:track, pid, topic, key, meta}, _from, state) do def handle_call({:track, pid, topic, key, meta}, _from, state) do
{:reply, :ok, track_pid(state, pid, topic, key, meta)} {:reply, :ok, track_pid(state, pid, topic, key, meta)}
end end
@ -105,16 +110,16 @@ defmodule Phoenix.Presence.Client do
|> Map.keys() |> Map.keys()
|> Enum.count() |> Enum.count()
leaved_key = Map.keys(leaves) |> hd left_key = Map.keys(leaves) |> hd
left_meta = leaves[leaved_key].metas |> hd left_meta = leaves[left_key].metas |> hd
state.client.handle_leave(topic, leaved_key, left_meta, state) state.client.handle_leave(topic, left_key, left_meta, state)
# if no more presences for given topic, unsubscribe # if no more presences for given topic, unsubscribe
if presences_count == 0 do if presences_count == 0 do
Phoenix.PubSub.unsubscribe(state.pubsub, topic) Phoenix.PubSub.unsubscribe(state.pubsub, topic)
update_topics_state(:remove_topic, state, topic, leaved_key) update_topics_state(:remove_topic, state, topic, left_key)
else else
update_topics_state(:remove_presence, state, topic, leaved_key) update_topics_state(:remove_presence, state, topic, left_key)
end end
end end

View file

@ -159,7 +159,7 @@ 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.assign.presences |> Enum.reject(fn user -> user.id == user_id end) updated_presences = socket.assigns.presences |> Enum.reject(fn user -> user.id == user_id end)
{:noreply, assign(socket, :presences, updated_presences)} {:noreply, assign(socket, :presences, updated_presences)}
end end