-
Notifications
You must be signed in to change notification settings - Fork 24
Task: Implement caching mechanism #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
53dfa78
add crashing component to dev
GuzekAlan 10a81a3
state_server added
GuzekAlan b50a2c5
broadcast state update
GuzekAlan 6e688f7
naming changes
GuzekAlan ae1542a
remove unnecessary subscribe
GuzekAlan 5371b76
small changes, revert ui changes
GuzekAlan 350f6fb
small fixes
GuzekAlan cf95997
use ChannelService for getting state
GuzekAlan 6e003cb
fix tests
GuzekAlan af65cb5
fix tests after rebase
GuzekAlan 467f6f0
wip
GuzekAlan d3f5cc9
change
GuzekAlan 10b4a5f
add tests
GuzekAlan dc4aab0
change state on component deleted
GuzekAlan 2ff70be
fix tests and publishing
GuzekAlan a0c2ff4
simplify code
GuzekAlan 0b02aba
cleaning
GuzekAlan 418f9ae
use status change to change components tree
GuzekAlan f312ac0
dont duplicate code
GuzekAlan 513e79f
revert node_changed handle_info deletion
GuzekAlan d1aade2
only one process_status message, use ChannelService in LvProcess
GuzekAlan 86928b2
fix tests
GuzekAlan a3f28b6
remove unused deleted_component topics
GuzekAlan c51a124
remove unused ts_f topic
GuzekAlan df84d64
Merge branch 'main' into 251-implement-caching-mechanism-ver2
GuzekAlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| defmodule LiveDebuggerDev.LiveComponents.Crash do | ||
| use DevWeb, :live_component | ||
|
|
||
| def render(assigns) do | ||
| ~H""" | ||
| <div> | ||
| <.box title="Crash [LiveComponent]" color="red"> | ||
| <.button phx-click="crash" color="red" phx-target={@myself}>Crash</.button> | ||
| </.box> | ||
| </div> | ||
| """ | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| defmodule LiveDebugger.GenServers.StateServer do | ||
| @moduledoc """ | ||
| This gen_server is responsible for storing the state of the application. | ||
| It collects state when `render` or `delete_component` callbacks are traced. | ||
| It uses named ETS table to store the state of the LiveView channel process. | ||
| When process dies, it removes the state from the table. | ||
| """ | ||
|
|
||
| use GenServer | ||
|
|
||
| alias LiveDebugger.Services.System.ProcessService | ||
| alias LiveDebugger.Utils.PubSub, as: PubSubUtils | ||
| alias LiveDebugger.CommonTypes | ||
| alias LiveDebugger.Structs.Trace | ||
|
|
||
| @ets_table_name :lvdbg_states | ||
|
|
||
| @callback get(pid :: pid()) :: {:ok, CommonTypes.channel_state()} | {:error, term()} | ||
|
|
||
| @doc """ | ||
| Returns previously stored state of the LiveView channel process identified by `pid`. | ||
| If the state is not found, it returns `{:error, :not_found}`. | ||
| """ | ||
| @spec get(pid :: pid()) :: {:ok, CommonTypes.channel_state()} | {:error, term()} | ||
| def get(pid) when is_pid(pid) do | ||
| impl().get(pid) | ||
| end | ||
|
|
||
| @doc false | ||
| @spec ets_table_name() :: atom() | ||
| def ets_table_name(), do: @ets_table_name | ||
|
|
||
| @doc false | ||
| def record_id(pid), do: "#{inspect(pid)}" | ||
|
|
||
| @doc false | ||
| def start_link(args \\ []) do | ||
| GenServer.start_link(__MODULE__, args, name: __MODULE__) | ||
| end | ||
|
|
||
| @impl true | ||
| def init(_args) do | ||
| :ets.new(@ets_table_name, [:named_table, :public, :ordered_set]) | ||
|
|
||
| PubSubUtils.node_rendered_topic() | ||
| |> PubSubUtils.subscribe!() | ||
|
|
||
| PubSubUtils.component_deleted_topic() | ||
| |> PubSubUtils.subscribe!() | ||
|
|
||
| PubSubUtils.process_status_topic() | ||
| |> PubSubUtils.subscribe!() | ||
|
|
||
| {:ok, []} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_info({:component_deleted, trace}, state) do | ||
| save_state(trace) | ||
|
|
||
| {:noreply, state} | ||
| end | ||
|
|
||
| def handle_info({:render_trace, trace}, state) do | ||
| save_state(trace) | ||
|
|
||
| {:noreply, state} | ||
| end | ||
|
|
||
| def handle_info({:process_status, {:dead, pid}}, state) do | ||
| :ets.delete(@ets_table_name, record_id(pid)) | ||
|
|
||
| {:noreply, state} | ||
| end | ||
|
|
||
| defp save_state(%Trace{pid: pid} = trace) do | ||
| with {:ok, channel_state} <- ProcessService.state(pid) do | ||
| record_id = record_id(pid) | ||
| :ets.insert(@ets_table_name, {record_id, channel_state}) | ||
|
|
||
| publish_state_changed(trace, channel_state) | ||
| end | ||
| end | ||
|
|
||
| defp publish_state_changed(%Trace{} = trace, channel_state) do | ||
| socket_id = trace.socket_id | ||
| transport_pid = trace.transport_pid | ||
| node_id = trace.cid || trace.pid | ||
|
|
||
| PubSubUtils.state_changed_topic(socket_id, transport_pid, node_id) | ||
|
GuzekAlan marked this conversation as resolved.
|
||
| |> PubSubUtils.broadcast({:state_changed, channel_state, trace}) | ||
|
|
||
| PubSubUtils.state_changed_topic(socket_id, transport_pid) | ||
| |> PubSubUtils.broadcast({:state_changed, channel_state, trace}) | ||
| end | ||
|
|
||
| defp impl() do | ||
| Application.get_env(:live_debugger, :state_server, __MODULE__.Impl) | ||
| end | ||
|
|
||
| defmodule Impl do | ||
| @moduledoc false | ||
|
|
||
| @behaviour LiveDebugger.GenServers.StateServer | ||
| @server_module LiveDebugger.GenServers.StateServer | ||
|
|
||
| def get(pid) do | ||
| case :ets.lookup(@server_module.ets_table_name(), @server_module.record_id(pid)) do | ||
| [{_, channel_state}] -> {:ok, channel_state} | ||
| [] -> {:error, :not_found} | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.