Skip to content
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

Building module using AST instead of strings #97

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 17 additions & 30 deletions lib/live_view_native/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,32 @@ defmodule LiveViewNative.Templates do
end

defp dump_class_tree_bytecode(class_tree_map, caller) do
class_tree_map
|> generate_class_tree_module(caller)
|> Code.compile_string()

:ok
generate_class_tree_module(class_tree_map, caller)
end

defp generate_class_tree_module(class_tree_map, caller) do
%Macro.Env{module: template_module, requires: requires} = caller
module_name = generate_class_tree_module_name(template_module)
branches = get_class_tree_branches(requires)

try do
:ets.new(:live_view_native_class_maps, [:public, :named_table])
rescue
_ -> :already_defined
ast = quote location: :keep do
def class_tree(stylesheet_key) do
%{
branches: unquote(branches),
contents: unquote(Macro.escape(class_tree_map))[stylesheet_key],
expanded_branches: [unquote(module_name)]
} ||
%{
branches: [],
contents: %{},
expanded_branches: [unquote(module_name)]
}
end
end

:ets.insert(:live_view_native_class_maps, {module_name, class_tree_map})

Macro.to_string(
quote location: :keep do
defmodule unquote(module_name) do
def class_tree(stylesheet_key) do
[_key, class_tree_map] = :ets.lookup(:live_view_native_class_maps, unquote(module_name))
%{
branches: unquote(branches),
contents: class_tree_map[stylesheet_key],
expanded_branches: [unquote(module_name)]
} ||
%{
branches: [],
contents: %{},
expanded_branches: [unquote(module_name)]
}
end
end
end
)
Module.create(module_name, ast, Macro.Env.location(__ENV__))

:ok
end

defp generate_class_tree_module_name(module) do
Expand Down
Loading