|
| 1 | +defmodule Phoenix.LiveView.Debug do |
| 2 | + @moduledoc """ |
| 3 | + Functions for runtime introspection and debugging of LiveViews. |
| 4 | +
|
| 5 | + This module provides utilities for inspecting and debugging LiveView processes |
| 6 | + at runtime. It allows you to: |
| 7 | +
|
| 8 | + * List all currently connected LiveViews |
| 9 | + * Check if a process is a LiveView |
| 10 | + * Get the socket of a LiveView process |
| 11 | + * Inspect LiveComponents rendered in a LiveView |
| 12 | +
|
| 13 | + ## Examples |
| 14 | +
|
| 15 | + # List all LiveViews |
| 16 | + iex> Phoenix.LiveView.Debug.list_liveviews() |
| 17 | + [%{pid: #PID<0.123.0>, view: MyAppWeb.PostLive.Index, topic: "lv:12345678", transport_pid: #PID<0.122.0>}] |
| 18 | +
|
| 19 | + # Check if a process is a LiveView |
| 20 | + iex> Phoenix.LiveView.Debug.liveview_process?(pid(0,123,0)) |
| 21 | + true |
| 22 | +
|
| 23 | + # Get the socket of a LiveView process |
| 24 | + iex> Phoenix.LiveView.Debug.socket(pid(0,123,0)) |
| 25 | + {:ok, %Phoenix.LiveView.Socket{...}} |
| 26 | +
|
| 27 | + # Get information about LiveComponents |
| 28 | + iex> Phoenix.LiveView.Debug.live_components(pid(0,123,0)) |
| 29 | + {:ok, [%{id: "component-1", module: MyAppWeb.PostLive.Index.Component1, ...}]} |
| 30 | +
|
| 31 | + """ |
| 32 | + |
| 33 | + @doc """ |
| 34 | + Returns a list of all currently connected LiveView processes (on the current node). |
| 35 | +
|
| 36 | + Each entry is a map with the following keys: |
| 37 | +
|
| 38 | + - `pid`: The PID of the LiveView process. |
| 39 | + - `view`: The module of the LiveView. |
| 40 | + - `topic`: The topic of the LiveView's channel. |
| 41 | + - `transport_pid`: The PID of the transport process. |
| 42 | +
|
| 43 | + The `transport_pid` can be used to group LiveViews on the same page. |
| 44 | +
|
| 45 | + ## Examples |
| 46 | +
|
| 47 | + iex> list_liveviews() |
| 48 | + [%{pid: #PID<0.123.0>, view: MyAppWeb.PostLive.Index, topic: "lv:12345678", transport_pid: #PID<0.122.0>}] |
| 49 | +
|
| 50 | + """ |
| 51 | + def list_liveviews do |
| 52 | + for pid <- Process.list(), dict = lv_process_dict(pid), not is_nil(dict) do |
| 53 | + {Phoenix.LiveView, view, topic} = keyfind(dict, :"$process_label") |
| 54 | + %{pid: pid, view: view, topic: topic, transport_pid: keyfind(dict, :"$phx_transport_pid")} |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + defp keyfind(list, key) do |
| 59 | + case List.keyfind(list, key, 0) do |
| 60 | + {^key, value} -> value |
| 61 | + _ -> nil |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + defp lv_process_dict(pid) do |
| 66 | + # LiveViews set the "$process_label" to {Phoenix.LiveView, view, topic} |
| 67 | + with info when is_list(info) <- Process.info(pid, [:dictionary]), |
| 68 | + dictionary when not is_nil(dictionary) <- keyfind(info, :dictionary), |
| 69 | + label when not is_nil(label) <- keyfind(dictionary, :"$process_label"), |
| 70 | + {Phoenix.LiveView, view, topic} when is_atom(view) and is_binary(topic) <- label do |
| 71 | + dictionary |
| 72 | + else |
| 73 | + _ -> nil |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + @doc """ |
| 78 | + Checks if the given pid is a LiveView process. |
| 79 | +
|
| 80 | + ## Examples |
| 81 | +
|
| 82 | + iex> list_liveviews() |> Enum.at(0) |> Map.fetch!(:pid) |> liveview_process?() |
| 83 | + true |
| 84 | +
|
| 85 | + iex> liveview_process?(pid(0,456,0)) |
| 86 | + false |
| 87 | +
|
| 88 | + """ |
| 89 | + def liveview_process?(pid) do |
| 90 | + not is_nil(lv_process_dict(pid)) |
| 91 | + end |
| 92 | + |
| 93 | + @doc """ |
| 94 | + Returns the socket of the LiveView process. |
| 95 | +
|
| 96 | + ## Examples |
| 97 | +
|
| 98 | + iex> list_liveviews() |> Enum.at(0) |> Map.fetch!(:pid) |> socket() |
| 99 | + {:ok, %Phoenix.LiveView.Socket{...}} |
| 100 | +
|
| 101 | + iex> socket(pid(0,123,0)) |
| 102 | + {:error, :not_alive_or_not_a_liveview} |
| 103 | +
|
| 104 | + """ |
| 105 | + def socket(liveview_pid) do |
| 106 | + GenServer.call(liveview_pid, {:phoenix, :debug_get_socket}) |
| 107 | + catch |
| 108 | + :exit, _ -> {:error, :not_alive_or_not_a_liveview} |
| 109 | + end |
| 110 | + |
| 111 | + @doc """ |
| 112 | + Returns a list with information about all LiveComponents rendered in the LiveView. |
| 113 | +
|
| 114 | + ## Examples |
| 115 | +
|
| 116 | + iex> live_components(pid) |
| 117 | + {:ok, |
| 118 | + [ |
| 119 | + %{ |
| 120 | + id: "component-1", |
| 121 | + module: MyAppWeb.PostLive.Index.Component1, |
| 122 | + cid: 1, |
| 123 | + assigns: %{ |
| 124 | + id: "component-1", |
| 125 | + __changed__: %{}, |
| 126 | + flash: %{}, |
| 127 | + myself: %Phoenix.LiveComponent.CID{cid: 1}, |
| 128 | + ... |
| 129 | + } |
| 130 | + } |
| 131 | + ]} |
| 132 | +
|
| 133 | + """ |
| 134 | + def live_components(liveview_pid) do |
| 135 | + GenServer.call(liveview_pid, {:phoenix, :debug_live_components}) |
| 136 | + rescue |
| 137 | + _ -> {:error, :not_alive_or_not_a_liveview} |
| 138 | + end |
| 139 | +end |
0 commit comments