Skip to content

Commit 3740e1e

Browse files
authored
add telemetry event after destroying a live component (#3777)
* 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. * [:phoenix, :live_component, :destroyed]
1 parent 18231cd commit 3740e1e

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

guides/server/telemetry.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,15 @@ LiveView currently exposes the following [`telemetry`](https://hexdocs.pm/teleme
300300
event: String.t(),
301301
params: unsigned_params
302302
}
303+
304+
* `[:phoenix, :live_component, :destroyed]` - Dispatched by a `Phoenix.LiveComponent`
305+
after it is destroyed. No measurement.
306+
307+
* Metadata:
308+
309+
%{
310+
socket: Phoenix.LiveView.Socket.t,
311+
component: atom,
312+
cid: integer(),
313+
live_view_socket: Phoenix.LiveView.Socket.t
314+
}

lib/phoenix_live_view/channel.ex

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,12 +1528,21 @@ defmodule Phoenix.LiveView.Channel do
15281528
{deleted_cids, new_components} = Diff.delete_component(cid, acc.components)
15291529

15301530
canceled_confs =
1531-
deleted_cids
1532-
|> Enum.filter(fn deleted_cid -> deleted_cid in upload_cids end)
1533-
|> Enum.flat_map(fn deleted_cid ->
1534-
read_socket(acc, deleted_cid, fn c_socket, _ ->
1535-
{_new_c_socket, canceled_confs} = Upload.maybe_cancel_uploads(c_socket)
1536-
canceled_confs
1531+
Enum.flat_map(deleted_cids, fn deleted_cid ->
1532+
read_socket(acc, deleted_cid, fn c_socket, component ->
1533+
:telemetry.execute([:phoenix, :live_component, :destroyed], %{}, %{
1534+
socket: c_socket,
1535+
component: component,
1536+
cid: deleted_cid,
1537+
live_view_socket: acc.socket
1538+
})
1539+
1540+
if deleted_cid in upload_cids do
1541+
{_new_c_socket, canceled_confs} = Upload.maybe_cancel_uploads(c_socket)
1542+
canceled_confs
1543+
else
1544+
[]
1545+
end
15371546
end)
15381547
end)
15391548

test/phoenix_live_view/integrations/live_components_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ defmodule Phoenix.LiveView.LiveComponentsTest do
7171
end
7272

7373
test "tracks removals", %{conn: conn} do
74+
ref =
75+
:telemetry_test.attach_event_handlers(self(), [[:phoenix, :live_component, :destroyed]])
76+
7477
{:ok, view, html} = live(conn, "/components")
7578

7679
assert [
@@ -101,6 +104,14 @@ defmodule Phoenix.LiveView.LiveComponentsTest do
101104
|> TreeDOM.normalize_to_tree(sort_attributes: true)
102105

103106
refute view |> element("#chris") |> has_element?()
107+
108+
assert_received {[:phoenix, :live_component, :destroyed], ^ref, _,
109+
%{
110+
component: StatefulComponent,
111+
cid: 1,
112+
socket: %{assigns: %{name: "chris"}},
113+
live_view_socket: %{assigns: %{names: ["jose"]}}
114+
}}
104115
end
105116

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

0 commit comments

Comments
 (0)