Skip to content

Commit

Permalink
Extract mount to ViewUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Serabe committed Dec 6, 2024
1 parent b7c794e commit 4f6141d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/live_isolated_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule LiveIsolatedComponent do
}
end)

live_isolated(build_conn(), LiveIsolatedComponent.View,
live_isolated(build_conn(), Keyword.get(opts, :mock_view, LiveIsolatedComponent.View),
session: %{
unquote(LiveIsolatedComponent.MessageNames.store_agent_key()) => store_agent
}
Expand Down
4 changes: 1 addition & 3 deletions lib/live_isolated_component/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ defmodule LiveIsolatedComponent.Utils do
def update_socket_from_store_agent(socket) do
agent = store_agent_pid(socket)

component = StoreAgent.get_component(agent)

socket
|> assign(:assigns, StoreAgent.get_assigns(agent))
|> assign(:component, component)
|> assign(:component, StoreAgent.get_component(agent))
|> assign(:slots, StoreAgent.get_slots(agent))
end

Expand Down
32 changes: 2 additions & 30 deletions lib/live_isolated_component/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ defmodule LiveIsolatedComponent.View do
alias LiveIsolatedComponent.Hooks
alias LiveIsolatedComponent.StoreAgent
alias LiveIsolatedComponent.Utils
alias LiveIsolatedComponent.ViewUtils
alias Phoenix.LiveView.TagEngine

def mount(params, session, socket) do
socket =
socket
|> assign(:store_agent, session[LiveIsolatedComponent.MessageNames.store_agent_key()])
|> run_on_mount(params, session)
|> Utils.update_socket_from_store_agent()

{:ok, socket}
end
def mount(params, session, socket), do: ViewUtils.mount(params, session, socket)

def render(%{component: component, store_agent: agent, assigns: component_assigns} = _assigns)
when is_function(component) do
Expand Down Expand Up @@ -77,25 +70,4 @@ defmodule LiveIsolatedComponent.View do

defp denormalize_result({:reply, map, socket}, original_assigns),
do: {:reply, map, Utils.denormalize_socket(socket, original_assigns)}

defp run_on_mount(socket, params, session),
do: run_on_mount(socket.assigns.store_agent, params, session, socket)

defp run_on_mount(agent, params, session, socket) do
agent
|> StoreAgent.get_on_mount()
|> add_lic_hooks()
|> Enum.reduce(socket, &do_run_on_mount(&1, params, session, &2))
end

defp do_run_on_mount({module, first}, params, session, socket) do
{:cont, socket} = module.on_mount(first, params, session, socket)
socket
end

defp do_run_on_mount(module, params, session, socket),
do: do_run_on_mount({module, :default}, params, session, socket)

defp add_lic_hooks(list),
do: [Hooks.HandleEventSpyHook, Hooks.HandleInfoSpyHook, Hooks.AssignsUpdateSpyHook | list]
end
49 changes: 49 additions & 0 deletions lib/live_isolated_component/view_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule LiveIsolatedComponent.ViewUtils do
@moduledoc """
Collection of utils for people that want to write their own
mock LiveView to use with `m:LiveIsolatedComponent.live_isolated_component/2`.
"""

alias LiveIsolatedComponent.Hooks
alias LiveIsolatedComponent.MessageNames
alias LiveIsolatedComponent.StoreAgent
alias LiveIsolatedComponent.Utils

@doc """
Run this in your mock view `c:Phoenix.LiveView.mount/3`.
## Options
- `:on_mmount`, _boolean_, defaults to `true`. Can disable adding `on_mount` hooks.
"""
def mount(params, session, socket, opts \\ []) do
socket =
socket
|> assign(:store_agent, session[MessageNames.store_agent_key()])

Check failure on line 21 in lib/live_isolated_component/view_utils.ex

View workflow job for this annotation

GitHub Actions / Build and test (1.14.5, 24.3, 0.19.0)

** (CompileError) lib/live_isolated_component/view_utils.ex:21: undefined function assign/3 (expected LiveIsolatedComponent.ViewUtils to define such a function or for it to be imported, but none are available)

Check failure on line 21 in lib/live_isolated_component/view_utils.ex

View workflow job for this annotation

GitHub Actions / Build and test (1.14.5, 24.3, 0.20.0)

** (CompileError) lib/live_isolated_component/view_utils.ex:21: undefined function assign/3 (expected LiveIsolatedComponent.ViewUtils to define such a function or for it to be imported, but none are available)
|> run_on_mount(params, session, opts)
|> Utils.update_socket_from_store_agent()

{:ok, socket}
end

defp run_on_mount(socket, params, session, opts),
do: run_on_mount(socket.assigns.store_agent, params, session, socket, opts)

defp run_on_mount(agent, params, session, socket, opts) do
on_mount = if Keyword.get(opts, :on_mount, true), do: StoreAgent.get_on_mount(agent), else: []

on_mount
|> add_lic_hooks()
|> Enum.reduce(socket, &do_run_on_mount(&1, params, session, &2))
end

defp do_run_on_mount({module, first}, params, session, socket) do
{:cont, socket} = module.on_mount(first, params, session, socket)
socket
end

defp do_run_on_mount(module, params, session, socket),
do: do_run_on_mount({module, :default}, params, session, socket)

defp add_lic_hooks(list),
do: [Hooks.HandleEventSpyHook, Hooks.HandleInfoSpyHook, Hooks.AssignsUpdateSpyHook | list]
end

0 comments on commit 4f6141d

Please sign in to comment.