Skip to content

Commit

Permalink
encapsulate router table
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Aug 18, 2023
1 parent 9da5b3b commit 57754f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/beacon/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ defmodule Beacon.Application do
Beacon.Repo
]

# We store routes by order and length so the most visited pages will likely be in the first rows
:ets.new(:beacon_pages, [:ordered_set, :named_table, :public, read_concurrency: true])
Beacon.Router.init()

:ets.new(:beacon_assets, [:set, :named_table, :public, read_concurrency: true])

Expand Down
18 changes: 13 additions & 5 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ defmodule Beacon.Router do
In your app router, add `use Beacon.Router` and call one the of the available macros.
"""

@ets_table :beacon_router

@doc false
# We store routes by order and length so the most visited pages will likely be in the first rows
def init do
:ets.new(@ets_table, [:ordered_set, :named_table, :public, read_concurrency: true])
end

defmacro __using__(_opts) do
quote do
unquote(prelude())
Expand Down Expand Up @@ -183,7 +191,7 @@ defmodule Beacon.Router do

@doc false
def add_page(site, path, {_page_id, _layout_id, _format, _page_module, _component_module} = metadata) do
add_page(:beacon_pages, site, path, metadata)
add_page(@ets_table, site, path, metadata)
end

@doc false
Expand All @@ -193,20 +201,20 @@ defmodule Beacon.Router do

@doc false
def del_page(site, path) do
:ets.delete(:beacon_pages, {site, path})
:ets.delete(@ets_table, {site, path})
end

@doc false
def dump_pages do
case :ets.match(:beacon_pages, :"$1") do
case :ets.match(@ets_table, :"$1") do
[] -> []
[pages] -> pages
end
end

@doc false
def lookup_path(site, path) do
lookup_path(:beacon_pages, site, path)
lookup_path(@ets_table, site, path)
end

# Lookup for a path stored in ets that is coming from a live view.
Expand Down Expand Up @@ -313,7 +321,7 @@ defmodule Beacon.Router do

@doc false
def path_params(page_path, path_info) do
page_path = String.split(page_path, "/")
page_path = String.split(page_path, "/", [])

Enum.zip_reduce(page_path, path_info, %{}, fn
":" <> segment, value, acc ->
Expand Down

0 comments on commit 57754f3

Please sign in to comment.