diff --git a/assets/js/app.js b/assets/js/app.js index 7abeb7f..08093cd 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -195,6 +195,7 @@ window.addEventListener("phx:page-loading-stop", info => topbar.hide()) window.addEventListener("phx:page-loading-stop", routeUpdated) window.addEventListener("js:exec", e => e.target[e.detail.call](...e.detail.args)) +window.addEventListener("phx:remove-el", e => document.getElementById(e.detail.id).remove()) // connect if there are any LiveViews on the page liveSocket.getSocket().onOpen(() => execJS("#connection-status", "js-hide")) diff --git a/lib/live_beats/presence/phoenix_presence_client.ex b/lib/live_beats/presence/phoenix_presence_client.ex index c8187e8..afda887 100644 --- a/lib/live_beats/presence/phoenix_presence_client.ex +++ b/lib/live_beats/presence/phoenix_presence_client.ex @@ -2,8 +2,10 @@ defmodule Phoenix.Presence.Client do use GenServer @callback init(state :: term) :: {:ok, new_state :: term} - @callback handle_join(topic :: String.t(), key :: String.t(), meta :: [map()], state :: term) :: {:ok, term} - @callback handle_leave(topic :: String.t(), key :: String.t(), meta :: [map()], state :: term) :: {:ok, term} + @callback handle_join(topic :: String.t(), key :: String.t(), meta :: [map()], state :: term) :: + {:ok, term} + @callback handle_leave(topic :: String.t(), key :: String.t(), meta :: [map()], state :: term) :: + {:ok, term} @doc """ TODO @@ -19,7 +21,8 @@ defmodule Phoenix.Presence.Client do {:ok, name} -> GenServer.start_link(__MODULE__, opts, name: name) - :error -> GenServer.start_link(__MODULE__, opts) + :error -> + GenServer.start_link(__MODULE__, opts) end end @@ -113,7 +116,9 @@ defmodule Phoenix.Presence.Client do updated_state = update_topics_state(:add_new_presence_or_metas, state, topic, joined_key, joined_meta) - {:ok, updated_client_state} = state.client.handle_join(topic, joined_key, joined_meta, state.client_state) + {:ok, updated_client_state} = + state.client.handle_join(topic, joined_key, meta, state.client_state) + updated_state = Map.put(updated_state, :client_state, updated_client_state) {updated_state, topic} @@ -122,7 +127,9 @@ defmodule Phoenix.Presence.Client do defp handle_leave({left_key, meta}, {state, topic}) do updated_state = update_topics_state(:remove_presence_or_metas, state, topic, left_key, meta) - {:ok, updated_client_state} = state.client.handle_leave(topic, left_key, meta, state.client_state) + {:ok, updated_client_state} = + state.client.handle_leave(topic, left_key, meta, state.client_state) + updated_state = Map.put(updated_state, :client_state, updated_client_state) {updated_state, topic} diff --git a/lib/live_beats/presence/presence_client.ex b/lib/live_beats/presence/presence_client.ex index 17280b2..0ddec5e 100644 --- a/lib/live_beats/presence/presence_client.ex +++ b/lib/live_beats/presence/presence_client.ex @@ -15,25 +15,34 @@ defmodule LiveBeats.PresenceClient do end @impl Phoenix.Presence.Client - def handle_join(topic, key, _meta, state) do + def handle_join(topic, _key, presence, state) do active_users_topic = topic |> profile_identifier() |> active_users_topic() - Phoenix.PubSub.local_broadcast(@pubsub, active_users_topic, {__MODULE__, %{user_joined: key}}) + Phoenix.PubSub.local_broadcast( + @pubsub, + active_users_topic, + {__MODULE__, %{user_joined: presence}} + ) {:ok, state} end @impl Phoenix.Presence.Client - def handle_leave(topic, key, _meta, state) do + def handle_leave(topic, _key, presence, state) do active_users_topic = topic |> profile_identifier() |> active_users_topic() - Phoenix.PubSub.local_broadcast(@pubsub, active_users_topic, {__MODULE__, %{user_left: key}}) + Phoenix.PubSub.local_broadcast( + @pubsub, + active_users_topic, + {__MODULE__, %{user_left: presence}} + ) + {:ok, state} end diff --git a/lib/live_beats_web/channels/presence.ex b/lib/live_beats_web/channels/presence.ex index 7593efc..d203309 100644 --- a/lib/live_beats_web/channels/presence.ex +++ b/lib/live_beats_web/channels/presence.ex @@ -5,8 +5,9 @@ defmodule LiveBeatsWeb.Presence do See the [`Phoenix.Presence`](http://hexdocs.pm/phoenix/Phoenix.Presence.html) docs for more details. """ - use Phoenix.Presence, otp_app: :live_beats, - pubsub_server: LiveBeats.PubSub + use Phoenix.Presence, + otp_app: :live_beats, + pubsub_server: LiveBeats.PubSub import Phoenix.LiveView.Helpers import LiveBeatsWeb.LiveHelpers @@ -18,10 +19,16 @@ defmodule LiveBeatsWeb.Presence do ~H"""
-

Who's Listening

-