Skip to content

Commit

Permalink
Additional refactoring of LiveViewNative.Component
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Jan 10, 2025
1 parent d025efb commit 7e63b1a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/live_view_native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ defmodule LiveViewNative do
Follows the same return types as `Map.fetch/2`
"""
def fetch_plugin(nil),
do: :error
def fetch_plugin(format) do
Map.fetch(plugins(), stringify(format))
end
Expand Down
26 changes: 16 additions & 10 deletions lib/live_view_native/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,28 @@ defmodule LiveViewNative.Component do
{opts, _} = Code.eval_quoted(opts)
format = opts[:format]

plugin = case LiveViewNative.fetch_plugin(format) do
{:ok, plugin} -> plugin
:error -> %{component: module}
end

declarative_opts = Keyword.drop(opts, [:as, :format, :root])
Module.put_attribute(module, :native_opts, %{
as: opts[:as],
format: format,
root: opts[:root]
})

plugin = case LiveViewNative.fetch_plugin(format) do
{:ok, plugin} -> plugin
:error -> %{component: module}
end

component = if module == plugin.component do
quote do
import LiveViewNative.Component, only: [sigil_LVN: 2]
end
else
quote do
use unquote(plugin.component)
end
end

quote do
import Kernel, except: [def: 2, defp: 2]
import Phoenix.Component.Declarative, only: []
Expand Down Expand Up @@ -181,11 +191,7 @@ defmodule LiveViewNative.Component do
embed_templates: 2
]

unquote(if (module == plugin.component) do
quote do: (import LiveViewNative.Component, only: [sigil_LVN: 2])
else
quote do: use unquote(plugin.component)
end)
unquote(component)

if (unquote(opts[:as])) do
@before_compile LiveViewNative.Renderer
Expand Down
2 changes: 1 addition & 1 deletion test/support/clients/gameboy/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule LiveViewNativeTest.GameBoy.Component do

defmacro __using__(_) do
quote do
import LiveViewNativeTest.GameBoy.Component
import unquote(__MODULE__)
end
end

Expand Down

0 comments on commit 7e63b1a

Please sign in to comment.