Skip to content

Instantly share code, notes, and snippets.

@garthk
Last active November 5, 2020 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/621d0f2ebb8567720a027021e0e7d238 to your computer and use it in GitHub Desktop.
Save garthk/621d0f2ebb8567720a027021e0e7d238 to your computer and use it in GitHub Desktop.
Watch all Phoenix.LiveView and LiveComponent handle_event callbacks
(fn gl ->
:dbg.stop_clear()
repr = fn
%Phoenix.LiveView.Socket{} -> "socket"
v -> inspect(v)
end
{:ok, _} =
:dbg.tracer(
:process,
{fn
{:trace, pid, :call, {module, function, args}}, _ ->
IO.puts(gl, [
[inspect(pid), ": ", inspect(module), ".", Atom.to_string(function), "("],
args |> Enum.map(repr) |> Enum.intersperse(", "),
")"
])
end, :state}
)
:dbg.p(:all, [:call])
for {app, _desc, _version} <- :application.loaded_applications(),
{:ok, modules} = :application.get_key(app, :modules),
module <- modules,
Kernel.function_exported?(module, :__live__, 0),
do: :dbg.tpl(module, :handle_event, [{:_, [], []}])
end).(Process.group_leader())
@garthk
Copy link
Author

garthk commented Oct 28, 2020

Paste the above at the iex> prompt to print out the function calls being made to your handle_event callbacks:

MyApp.MyViewLive.handle_event("change", %{"_csrf_token" => "...", "param" => "value"}}, socket)

Works on remote prompts, because Process.group_leader(); see Pretty for more detail on how that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment