From 7e63b1a797e79ef567110c688e5ab4201a1b25dc Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Fri, 10 Jan 2025 11:19:03 -0500 Subject: [PATCH] Additional refactoring of LiveViewNative.Component --- lib/live_view_native.ex | 2 ++ lib/live_view_native/component.ex | 26 ++++++++++++++--------- test/support/clients/gameboy/component.ex | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/live_view_native.ex b/lib/live_view_native.ex index ddbdbf4..bcb9ff4 100644 --- a/lib/live_view_native.ex +++ b/lib/live_view_native.ex @@ -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 diff --git a/lib/live_view_native/component.ex b/lib/live_view_native/component.ex index 9c8c126..6786488 100644 --- a/lib/live_view_native/component.ex +++ b/lib/live_view_native/component.ex @@ -114,11 +114,6 @@ 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], @@ -126,6 +121,21 @@ defmodule LiveViewNative.Component do 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: [] @@ -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 diff --git a/test/support/clients/gameboy/component.ex b/test/support/clients/gameboy/component.ex index 8575295..042102a 100644 --- a/test/support/clients/gameboy/component.ex +++ b/test/support/clients/gameboy/component.ex @@ -3,7 +3,7 @@ defmodule LiveViewNativeTest.GameBoy.Component do defmacro __using__(_) do quote do - import LiveViewNativeTest.GameBoy.Component + import unquote(__MODULE__) end end