29 lines
837 B
Elixir
29 lines
837 B
Elixir
defmodule ChronoscopeWeb.IndexLive do
|
|
use ChronoscopeWeb, :live_view
|
|
|
|
alias Chronoscope.NTS
|
|
alias Chronoscope.NTS.KeyEstablishmentResponse
|
|
|
|
@topic Application.compile_env(:chronoscope, :nts_topic)
|
|
|
|
def mount(_params, _session, socket) do
|
|
ChronoscopeWeb.Endpoint.subscribe(@topic)
|
|
{:ok, assign(socket, %{clients: client_list()})}
|
|
end
|
|
|
|
def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do
|
|
{:noreply, update(socket, :clients, &update_client(&1, client))}
|
|
end
|
|
|
|
defp update_client(client_list, client) do
|
|
# todo - use a map instead of list for fast lookups?
|
|
Enum.map(client_list, &if(client.server == &1.server, do: client, else: &1))
|
|
end
|
|
|
|
defp client_list() do
|
|
Chronoscope.ViewActivator
|
|
|> GenServer.call(:get_nts_servers)
|
|
|> NTS.list_clients()
|
|
end
|
|
end
|