|
| 1 | +defmodule LiveDebugger.GenServers.StateServer do |
| 2 | + @moduledoc """ |
| 3 | + This gen_server is responsible for storing the state of the application. |
| 4 | + It collects state when `render` or `delete_component` callbacks are traced. |
| 5 | + It uses named ETS table to store the state of the LiveView channel process. |
| 6 | + When process dies, it removes the state from the table. |
| 7 | + """ |
| 8 | + |
| 9 | + use GenServer |
| 10 | + |
| 11 | + alias LiveDebugger.Services.System.ProcessService |
| 12 | + alias LiveDebugger.Utils.PubSub, as: PubSubUtils |
| 13 | + alias LiveDebugger.CommonTypes |
| 14 | + alias LiveDebugger.Structs.Trace |
| 15 | + |
| 16 | + @ets_table_name :lvdbg_states |
| 17 | + |
| 18 | + @callback get(pid :: pid()) :: {:ok, CommonTypes.channel_state()} | {:error, term()} |
| 19 | + |
| 20 | + @doc """ |
| 21 | + Returns previously stored state of the LiveView channel process identified by `pid`. |
| 22 | + If the state is not found, it returns `{:error, :not_found}`. |
| 23 | + """ |
| 24 | + @spec get(pid :: pid()) :: {:ok, CommonTypes.channel_state()} | {:error, term()} |
| 25 | + def get(pid) when is_pid(pid) do |
| 26 | + impl().get(pid) |
| 27 | + end |
| 28 | + |
| 29 | + @doc false |
| 30 | + @spec ets_table_name() :: atom() |
| 31 | + def ets_table_name(), do: @ets_table_name |
| 32 | + |
| 33 | + @doc false |
| 34 | + def record_id(pid), do: "#{inspect(pid)}" |
| 35 | + |
| 36 | + @doc false |
| 37 | + def start_link(args \\ []) do |
| 38 | + GenServer.start_link(__MODULE__, args, name: __MODULE__) |
| 39 | + end |
| 40 | + |
| 41 | + @impl true |
| 42 | + def init(_args) do |
| 43 | + :ets.new(@ets_table_name, [:named_table, :public, :ordered_set]) |
| 44 | + |
| 45 | + PubSubUtils.node_rendered_topic() |
| 46 | + |> PubSubUtils.subscribe!() |
| 47 | + |
| 48 | + PubSubUtils.component_deleted_topic() |
| 49 | + |> PubSubUtils.subscribe!() |
| 50 | + |
| 51 | + PubSubUtils.process_status_topic() |
| 52 | + |> PubSubUtils.subscribe!() |
| 53 | + |
| 54 | + {:ok, []} |
| 55 | + end |
| 56 | + |
| 57 | + @impl true |
| 58 | + def handle_info({:component_deleted, trace}, state) do |
| 59 | + save_state(trace) |
| 60 | + |
| 61 | + {:noreply, state} |
| 62 | + end |
| 63 | + |
| 64 | + def handle_info({:render_trace, trace}, state) do |
| 65 | + save_state(trace) |
| 66 | + |
| 67 | + {:noreply, state} |
| 68 | + end |
| 69 | + |
| 70 | + def handle_info({:process_status, {:dead, pid}}, state) do |
| 71 | + :ets.delete(@ets_table_name, record_id(pid)) |
| 72 | + |
| 73 | + {:noreply, state} |
| 74 | + end |
| 75 | + |
| 76 | + defp save_state(%Trace{pid: pid} = trace) do |
| 77 | + with {:ok, channel_state} <- ProcessService.state(pid) do |
| 78 | + record_id = record_id(pid) |
| 79 | + :ets.insert(@ets_table_name, {record_id, channel_state}) |
| 80 | + |
| 81 | + publish_state_changed(trace, channel_state) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + defp publish_state_changed(%Trace{} = trace, channel_state) do |
| 86 | + socket_id = trace.socket_id |
| 87 | + transport_pid = trace.transport_pid |
| 88 | + node_id = trace.cid || trace.pid |
| 89 | + |
| 90 | + PubSubUtils.state_changed_topic(socket_id, transport_pid, node_id) |
| 91 | + |> PubSubUtils.broadcast({:state_changed, channel_state, trace}) |
| 92 | + |
| 93 | + PubSubUtils.state_changed_topic(socket_id, transport_pid) |
| 94 | + |> PubSubUtils.broadcast({:state_changed, channel_state, trace}) |
| 95 | + end |
| 96 | + |
| 97 | + defp impl() do |
| 98 | + Application.get_env(:live_debugger, :state_server, __MODULE__.Impl) |
| 99 | + end |
| 100 | + |
| 101 | + defmodule Impl do |
| 102 | + @moduledoc false |
| 103 | + |
| 104 | + @behaviour LiveDebugger.GenServers.StateServer |
| 105 | + @server_module LiveDebugger.GenServers.StateServer |
| 106 | + |
| 107 | + def get(pid) do |
| 108 | + case :ets.lookup(@server_module.ets_table_name(), @server_module.record_id(pid)) do |
| 109 | + [{_, channel_state}] -> {:ok, channel_state} |
| 110 | + [] -> {:error, :not_found} |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments