Improve docs & readme & small refactor

This commit is contained in:
Max Veytsman
2022-09-02 13:49:03 -04:00
parent 65fb8c6988
commit cf2401336d
8 changed files with 49 additions and 18 deletions

View File

@ -1,11 +1,18 @@
defmodule Heroicons.IconCache do
@doc "Get's an icon's body from the filesystem"
defmodule Heroicons.Cache do
@moduledoc """
An ETS-backed cache for icons. We cache both pre-compiled Phoenix Components and icon bodies as strings.
Uses the icon's path on disk as a key.
"""
use GenServer
@name __MODULE__
@doc false
def start_link(_), do: GenServer.start_link(__MODULE__, [], name: @name)
@doc "Fetch a pre-compiled Phoenix Component from the cache or disk, given a `path`"
def fetch_component(path) do
case :ets.lookup(@name.Components, path) do
[{^path, component}] ->
@ -16,6 +23,7 @@ defmodule Heroicons.IconCache do
end
end
@doc "Fetch a icon's body from the cache or disk, given a `path`"
def fetch_body(path) do
case :ets.lookup(@name, path) do
[{^path, body}] ->
@ -26,6 +34,7 @@ defmodule Heroicons.IconCache do
end
end
@impl true
def init(_) do
:ets.new(@name, [:set, :protected, :named_table])
:ets.new(@name.Components, [:set, :protected, :named_table])
@ -33,6 +42,7 @@ defmodule Heroicons.IconCache do
{:ok, []}
end
@impl true
def handle_call({:cache_body, path}, _ref, state) do
body = read_body(path)

View File

@ -1,4 +1,5 @@
defmodule Heroicons.Generator do
@moduledoc false
defmacro __using__(icon_dir: icon_dir) do
icon_paths =
Path.absname(icon_dir, :code.priv_dir(:heroicons))
@ -73,7 +74,7 @@ defmodule Heroicons.Generator do
{component, _binding} =
Code.eval_quoted(
Heroicons.IconCache.fetch_component(path),
Heroicons.Cache.fetch_component(path),
assigns: assigns
)
@ -97,7 +98,7 @@ defmodule Heroicons.Generator do
"<svg",
Phoenix.HTML.Safe.to_iodata(attrs),
" ",
Heroicons.IconCache.fetch_body(path)
Heroicons.Cache.fetch_body(path)
]}
end
end

View File

@ -1,7 +1,6 @@
defmodule Heroicons.Outline do
@moduledoc """
Outline style icons drawn with a stroke, packaged as Phoenix Components.
For primary navigation and marketing sections, with an outlined appearance,
designed to be rendered at 24x24.
"""

View File

@ -1,7 +1,6 @@
defmodule Heroicons.Solid do
@moduledoc """
Solid style icons drawn with fills, packaged as Phoenix Components.
For primary navigation and marketing sections, with a filled appearance,
designed to be rendered at 24x24.
"""