Skip to content

Commit cf8ed8e

Browse files
committed
add telemetry event after destroying a live component
The people from [LiveDebugger](https://github.com/software-mansion/live-debugger) mentioned that they struggle to react to live components that are removed from a page. A new telemetry event should help.
1 parent fbed65d commit cf8ed8e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

guides/server/telemetry.md

+10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ LiveView currently exposes the following [`telemetry`](https://hexdocs.pm/teleme
191191
changed?: boolean
192192
}
193193

194+
* `[:phoenix, :live_view, :component_destroyed]` - Dispatched by a `Phoenix.LiveView`
195+
after a LiveComponent is destroyed. No measurement.
196+
197+
* Metadata:
198+
199+
%{
200+
socket: Phoenix.LiveView.Socket.t,
201+
cid: integer()
202+
}
203+
194204
* `[:phoenix, :live_component, :update, :start]` - Dispatched by a `Phoenix.LiveComponent`
195205
immediately before [`update/2`](`c:Phoenix.LiveComponent.update/2`) or a
196206
[`update_many/1`](`c:Phoenix.LiveComponent.update_many/1`) is invoked.

lib/phoenix_live_view/channel.ex

+5
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,11 @@ defmodule Phoenix.LiveView.Channel do
15271527
Enum.flat_map_reduce(cids, state, fn cid, acc ->
15281528
{deleted_cids, new_components} = Diff.delete_component(cid, acc.components)
15291529

1530+
:telemetry.execute([:phoenix, :live_view, :component_destroyed], %{}, %{
1531+
socket: state.socket,
1532+
cid: cid
1533+
})
1534+
15301535
canceled_confs =
15311536
deleted_cids
15321537
|> Enum.filter(fn deleted_cid -> deleted_cid in upload_cids end)

test/phoenix_live_view/integrations/live_components_test.exs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ defmodule Phoenix.LiveView.LiveComponentsTest do
7171
end
7272

7373
test "tracks removals", %{conn: conn} do
74+
ref = :telemetry_test.attach_event_handlers(self(), [[:phoenix, :live_view, :component_destroyed]])
75+
7476
{:ok, view, html} = live(conn, "/components")
7577

7678
assert [
@@ -101,6 +103,8 @@ defmodule Phoenix.LiveView.LiveComponentsTest do
101103
|> TreeDOM.normalize_to_tree(sort_attributes: true)
102104

103105
refute view |> element("#chris") |> has_element?()
106+
107+
assert_received {[:phoenix, :live_view, :component_destroyed], ^ref, _, %{socket: _, cid: 1}}
104108
end
105109

106110
test "tracks removals when whole root changes", %{conn: conn} do

0 commit comments

Comments
 (0)