mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-25 09:20:59 +00:00
fetch active users on mount
This commit is contained in:
parent
d93bb8e810
commit
b8608bc77d
2 changed files with 33 additions and 19 deletions
|
@ -1,7 +1,7 @@
|
||||||
defmodule LiveBeats.UserTracker do
|
defmodule LiveBeats.UserTracker do
|
||||||
use GenServer
|
use GenServer
|
||||||
@pubsub LiveBeats.PubSub
|
@pubsub LiveBeats.PubSub
|
||||||
@poll_interval :timer.seconds(5)
|
@poll_interval :timer.seconds(30)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
TODO
|
TODO
|
||||||
|
@ -15,6 +15,10 @@ defmodule LiveBeats.UserTracker do
|
||||||
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def list_active_users() do
|
||||||
|
GenServer.call(__MODULE__, :list_users)
|
||||||
|
end
|
||||||
|
|
||||||
def presence_joined(presence) do
|
def presence_joined(presence) do
|
||||||
GenServer.call(__MODULE__, {:presence_joined, presence})
|
GenServer.call(__MODULE__, {:presence_joined, presence})
|
||||||
end
|
end
|
||||||
|
@ -28,6 +32,11 @@ defmodule LiveBeats.UserTracker do
|
||||||
{:ok, schedule_updates(%{})}
|
{:ok, schedule_updates(%{})}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:list_users, _from, state) do
|
||||||
|
{:reply, list_users(state), state}
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_call({:presence_joined, presence}, _from, state) do
|
def handle_call({:presence_joined, presence}, _from, state) do
|
||||||
{:reply, :ok, handle_join(state, presence)}
|
{:reply, :ok, handle_join(state, presence)}
|
||||||
|
@ -70,11 +79,14 @@ defmodule LiveBeats.UserTracker do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp broadcast_updates(state) do
|
defp broadcast_updates(state) do
|
||||||
active_users =
|
Phoenix.PubSub.local_broadcast(
|
||||||
state
|
@pubsub,
|
||||||
|> Enum.map(fn {_key, value} -> value end)
|
topic(),
|
||||||
|
{LiveBeats.UserTracker, %{active_users: list_users(state)}}
|
||||||
Phoenix.PubSub.local_broadcast(@pubsub, topic(), {LiveBeats.UserTracker, %{active_users: active_users}})
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp list_users(state) do
|
||||||
|
Enum.map(state, fn {_key, value} -> value end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
defmodule LiveBeatsWeb.Nav do
|
defmodule LiveBeatsWeb.Nav do
|
||||||
import Phoenix.LiveView
|
import Phoenix.LiveView
|
||||||
|
alias LiveBeats.UserTracker
|
||||||
alias LiveBeatsWeb.{ProfileLive, SettingsLive}
|
alias LiveBeatsWeb.{ProfileLive, SettingsLive}
|
||||||
|
|
||||||
def on_mount(:default, _params, _session, socket) do
|
def on_mount(:default, _params, _session, socket) do
|
||||||
if connected?(socket) do
|
if connected?(socket) do
|
||||||
LiveBeats.UserTracker.subscribe()
|
UserTracker.subscribe()
|
||||||
end
|
end
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> assign(:active_users, [])
|
|> assign(:active_users, UserTracker.list_active_users())
|
||||||
|> assign(:region, System.get_env("FLY_REGION"))
|
|> assign(:region, System.get_env("FLY_REGION"))
|
||||||
|> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)
|
|> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)
|
||||||
|> attach_hook(:ping, :handle_event, &handle_event/3)
|
|> attach_hook(:ping, :handle_event, &handle_event/3)
|
||||||
|> attach_hook(:actie_users, :handle_info, fn
|
|> attach_hook(:actie_users, :handle_info, fn
|
||||||
{LiveBeats.UserTracker, %{active_users: users}}, socket ->
|
{LiveBeats.UserTracker, %{active_users: users}}, socket ->
|
||||||
{:halt, assign(socket, :active_users, users)}
|
{:halt, assign(socket, :active_users, users)}
|
||||||
|
|
||||||
_params, socket ->
|
_params, socket ->
|
||||||
{:cont, socket}
|
{:cont, socket}
|
||||||
|
|
||||||
end)
|
end)
|
||||||
{:cont, socket}
|
|
||||||
|
{:cont, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_active_tab_params(params, _url, socket) do
|
defp handle_active_tab_params(params, _url, socket) do
|
||||||
|
|
Loading…
Reference in a new issue