|
| 1 | +defmodule LiveDebugger.API.UserEvents do |
| 2 | + @moduledoc """ |
| 3 | + API for sending user-triggered events and messages to LiveView processes. |
| 4 | +
|
| 5 | + This module provides functions to interact with LiveView processes by sending |
| 6 | + various types of messages: component updates, info messages, GenServer casts/calls, |
| 7 | + and Phoenix LiveView events. |
| 8 | + """ |
| 9 | + |
| 10 | + alias LiveDebugger.Structs.LvProcess |
| 11 | + alias Phoenix.LiveComponent.CID |
| 12 | + alias LiveDebugger.CommonTypes |
| 13 | + |
| 14 | + @callback send_component_update(LvProcess.t(), CommonTypes.cid(), map()) :: :ok |
| 15 | + @callback send_info_message(LvProcess.t(), term()) :: term() |
| 16 | + @callback send_genserver_cast(LvProcess.t(), term()) :: :ok |
| 17 | + @callback send_genserver_call(LvProcess.t(), term()) :: term() |
| 18 | + @callback send_lv_event(LvProcess.t(), CommonTypes.cid() | nil, String.t(), map()) :: term() |
| 19 | + |
| 20 | + @doc """ |
| 21 | + Sends an update to a LiveComponent. |
| 22 | +
|
| 23 | + This will trigger the `update_many/1` callback if defined, otherwise falls back to `update/2`. |
| 24 | + See: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-update-many |
| 25 | + """ |
| 26 | + @spec send_component_update(LvProcess.t(), CommonTypes.cid(), map()) :: :ok |
| 27 | + def send_component_update(%LvProcess{} = lv_process, %CID{} = cid, payload) do |
| 28 | + impl().send_component_update(lv_process, cid, payload) |
| 29 | + end |
| 30 | + |
| 31 | + @doc """ |
| 32 | + Sends an info message directly to the LiveView process. |
| 33 | +
|
| 34 | + The message will be handled by the `handle_info/2` callback in the LiveView. |
| 35 | + """ |
| 36 | + @spec send_info_message(LvProcess.t(), term()) :: term() |
| 37 | + def send_info_message(%LvProcess{} = lv_process, payload) do |
| 38 | + impl().send_info_message(lv_process, payload) |
| 39 | + end |
| 40 | + |
| 41 | + @doc """ |
| 42 | + Sends a GenServer cast to the LiveView process. |
| 43 | +
|
| 44 | + The message will be handled by the `handle_cast/2` callback. |
| 45 | + """ |
| 46 | + @spec send_genserver_cast(LvProcess.t(), term()) :: :ok |
| 47 | + def send_genserver_cast(%LvProcess{} = lv_process, payload) do |
| 48 | + impl().send_genserver_cast(lv_process, payload) |
| 49 | + end |
| 50 | + |
| 51 | + @doc """ |
| 52 | + Sends a GenServer call to the LiveView process. |
| 53 | +
|
| 54 | + The message will be handled by the `handle_call/3` callback. |
| 55 | + Returns the response from the LiveView process. |
| 56 | + """ |
| 57 | + @spec send_genserver_call(LvProcess.t(), term()) :: term() |
| 58 | + def send_genserver_call(%LvProcess{} = lv_process, payload) do |
| 59 | + impl().send_genserver_call(lv_process, payload) |
| 60 | + end |
| 61 | + |
| 62 | + @doc """ |
| 63 | + Sends a Phoenix LiveView event to the LiveView process. |
| 64 | +
|
| 65 | + This simulates a user-triggered event (like a button click) and will be handled |
| 66 | + by the `handle_event/3` callback in the LiveView or LiveComponent. |
| 67 | +
|
| 68 | + ## Parameters |
| 69 | +
|
| 70 | + * `lv_process` - The target LiveView process |
| 71 | + * `cid` - Optional component ID (CID) if targeting a LiveComponent, `nil` for LiveView |
| 72 | + * `event` - The event name as a string |
| 73 | + * `params` - The event parameters as a map |
| 74 | + """ |
| 75 | + @spec send_lv_event(LvProcess.t(), CommonTypes.cid() | nil, String.t(), map()) :: term() |
| 76 | + def send_lv_event(%LvProcess{} = lv_process, cid \\ nil, event, params) do |
| 77 | + impl().send_lv_event(lv_process, cid, event, params) |
| 78 | + end |
| 79 | + |
| 80 | + defp impl do |
| 81 | + Application.get_env( |
| 82 | + :live_debugger, |
| 83 | + :api_user_events, |
| 84 | + __MODULE__.Impl |
| 85 | + ) |
| 86 | + end |
| 87 | + |
| 88 | + defmodule Impl do |
| 89 | + @moduledoc false |
| 90 | + @behaviour LiveDebugger.API.UserEvents |
| 91 | + |
| 92 | + alias LiveDebugger.Structs.LvProcess |
| 93 | + alias Phoenix.LiveComponent.CID |
| 94 | + |
| 95 | + @impl true |
| 96 | + def send_component_update(%LvProcess{} = lv_process, %CID{} = cid, payload) do |
| 97 | + Phoenix.LiveView.send_update(lv_process.pid, cid, payload) |
| 98 | + end |
| 99 | + |
| 100 | + @impl true |
| 101 | + def send_info_message(%LvProcess{} = lv_process, payload) do |
| 102 | + send(lv_process.pid, payload) |
| 103 | + end |
| 104 | + |
| 105 | + @impl true |
| 106 | + def send_genserver_cast(%LvProcess{} = lv_process, payload) do |
| 107 | + GenServer.cast(lv_process.pid, payload) |
| 108 | + end |
| 109 | + |
| 110 | + @impl true |
| 111 | + def send_genserver_call(%LvProcess{} = lv_process, payload) do |
| 112 | + GenServer.call(lv_process.pid, payload) |
| 113 | + end |
| 114 | + |
| 115 | + @impl true |
| 116 | + def send_lv_event(%LvProcess{} = lv_process, cid, event, params) do |
| 117 | + payload = %{"event" => event, "value" => params, "type" => "debug"} |
| 118 | + payload = if is_nil(cid), do: payload, else: Map.put(payload, "cid", cid.cid) |
| 119 | + |
| 120 | + message = %Phoenix.Socket.Message{ |
| 121 | + topic: "lv:#{lv_process.socket_id}", |
| 122 | + event: "event", |
| 123 | + payload: payload |
| 124 | + } |
| 125 | + |
| 126 | + send(lv_process.pid, message) |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments