-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlv_process.ex
More file actions
77 lines (67 loc) · 1.9 KB
/
Copy pathlv_process.ex
File metadata and controls
77 lines (67 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
defmodule LiveDebugger.Structs.LvProcess do
@moduledoc """
This module provides a struct to represent a LiveView process.
* nested? - whether the process is a nested LiveView process
* debugger? - whether the process is a LiveDebugger process
"""
defstruct [
:socket_id,
:root_pid,
:parent_pid,
:pid,
:transport_pid,
:module,
:nested?,
:debugger?,
:embedded?
]
@type t() :: %__MODULE__{
socket_id: String.t(),
root_pid: pid(),
parent_pid: pid() | nil,
pid: pid(),
transport_pid: pid(),
module: module(),
nested?: boolean(),
embedded?: boolean(),
debugger?: boolean()
}
@spec new(pid :: pid(), socket :: Phoenix.LiveView.Socket.t()) :: t()
def new(pid, socket) do
nested? = pid != socket.root_pid
debugger? =
socket.view
|> Atom.to_string()
|> String.starts_with?(["Elixir.LiveDebugger.", "Elixir.LiveDebuggerWeb."])
embedded? = socket.host_uri == :not_mounted_at_router
%__MODULE__{
socket_id: socket.id,
root_pid: socket.root_pid,
pid: pid,
parent_pid: socket.parent_pid,
transport_pid: socket.transport_pid,
module: socket.view,
nested?: nested?,
debugger?: debugger?,
embedded?: embedded?
}
end
@doc """
Creates new LvProcess struct with the given `pid` by fetching the socket from the process state.
"""
@spec new(pid :: pid()) :: t() | nil
def new(pid) do
case LiveDebugger.Services.ChannelService.state(pid) do
{:ok, %{socket: socket}} ->
new(pid, socket)
{:error, _} ->
nil
end
end
@doc """
Returns the parent LvProcess of the given `lv_process`.
"""
@spec parent(lv_process :: t()) :: t() | nil
def parent(%__MODULE__{parent_pid: nil}), do: nil
def parent(%__MODULE__{parent_pid: parent_pid}), do: new(parent_pid)
end