Skip to content

Commit

Permalink
Move stuff around due to adding compiler option
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira committed Jul 4, 2024
1 parent 41919b4 commit 6c9eb16
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 260 deletions.
2 changes: 1 addition & 1 deletion src/arizona.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(arizona).

-opaque payload() :: map().
-type payload() :: list().
-export_type([payload/0]).

-type route_opts() :: map().
Expand Down
3 changes: 1 addition & 2 deletions src/arizona_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
when StartType :: application:start_type(),
StartArgs :: term().
start(_StartType, _StartArgs) ->
{ok, _} = arizona_server:start(),
arizona_server:start(),
arizona_sup:start_link().

-spec stop(State) -> ok
when State :: term().
stop(_State) ->
ok.

3 changes: 1 addition & 2 deletions src/arizona_cfg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

-export([endpoint/0]).

-opaque endpoint() :: map().
-type endpoint() :: map().
-export_type([endpoint/0]).

-spec endpoint() -> endpoint().
endpoint() ->
application:get_env(arizona, endpoint, #{}).

1 change: 0 additions & 1 deletion src/arizona_html.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ to_safe(V) when is_float(V) ->
-spec safe_types() -> [binary | atom | integer | float].
safe_types() ->
[binary, atom, integer, float].

1 change: 0 additions & 1 deletion src/arizona_js.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ send(EventName, Payload) ->

safe(Term) ->
iolist_to_binary(json:encode(Term)).

3 changes: 1 addition & 2 deletions src/arizona_live_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
Opts :: arizona:route_opts().
init(Req0, {Mod, Fun, Opts} = State) ->
Macros = maps:get(macros, Opts, #{}),
Tpl = arizona_live_view:persist_get(Mod, Fun, Macros),
Tpl = arizona_tpl_compile:compile(Mod, Fun, Macros),
Assigns = maps:get(assigns, Opts, #{}),
Html = arizona_tpl_render:render_block(Tpl, Assigns),
Headers = #{<<"content-type">> => <<"text/html">>},
Req = cowboy_req:reply(200, Headers, Html, Req0),
{ok, Req, State}.

21 changes: 9 additions & 12 deletions src/arizona_live_reload.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Live-reload functionality for use during development.

%% State
-record(state, {
timer :: reference(),
files = #{} :: #{string() := {erl, modified}},
clients = #{} :: #{pid() := boolean()}
timer :: undefined | reference(),
files :: #{string() := {erl, modified}},
clients :: #{pid() := boolean()}
}).
-opaque state() :: #state{}.
-export_type([state/0]).
Expand Down Expand Up @@ -68,7 +68,10 @@ reload() ->
init(_Args) ->
register_events(),
setup_watcher(),
{ok, #state{}}.
{ok, #state{
files = #{},
clients = #{}
}}.

-spec handle_call(Request, From, State) -> no_return()
when Request :: term(),
Expand All @@ -94,12 +97,7 @@ handle_info({terminate, Client}, #state{clients = Clients} = State) ->
handle_info({_Pid, {fs, file_event}, {File, Events}},
#state{timer = Timer, files = Files} = State0) ->
% Try recompile only when the last modified file was received.
case Timer =:= undefined of
true ->
ok;
false ->
erlang:cancel_timer(Timer)
end,
Timer =/= undefined andalso erlang:cancel_timer(Timer),
State = State0#state{timer = erlang:send_after(25, self(), recompile)},
case filename:extension(File) of
".erl" ->
Expand Down Expand Up @@ -134,9 +132,8 @@ register_events() ->
arizona_websocket:subscribe(terminate).

setup_watcher() ->
fs:start_link(arizona_live_reload_fs, filename:absname("")),
{ok, _} = fs:start_link(arizona_live_reload_fs, filename:absname("")),
fs:subscribe(arizona_live_reload_fs).

reload(Clients) ->
maps:foreach(fun(Client, _) -> Client ! reload end, Clients).

41 changes: 11 additions & 30 deletions src/arizona_live_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,16 @@ Live view.
%% API functions.
-export([parse_str/2]).
-ignore_xref([parse_str/2]).
-export([compile/3]).
-ignore_xref([compile/3]).
-export([mount/2]).
-export([handle_event/4]).

-export([put_macro/3]).
-ignore_xref([put_macro/3]).
-export([get_macro/3]).
-ignore_xref([get_macro/3]).

-opaque macros() :: map().
-type macros() :: map().
-export_type([macros/0]).

-opaque tree() :: list().
-export_type([tree/0]).

%% --------------------------------------------------------------------
%% Callbacks.
%% --------------------------------------------------------------------
Expand All @@ -51,7 +45,7 @@ Live view.

-callback render(Macros) -> Tree
when Macros :: macros(),
Tree :: tree().
Tree :: arizona_tpl_parse:tree().

-callback handle_event(EventName, Payload, Socket) -> Socket
when EventName :: binary(),
Expand Down Expand Up @@ -81,22 +75,12 @@ put_macro(Key, Value, Macros) ->
get_macro(Key, Macros, Default) ->
maps:get(Key, Macros, Default).

-spec parse_str(Str, Macros) -> Parsed
-spec parse_str(Str, Macros) -> arizona_tpl_parse:tree()
when Str :: string() | binary(),
Macros :: macros(),
Parsed :: tree().
Macros :: macros().
parse_str(Str, Macros) ->
Tokens = arizona_tpl_scan:string(Str),
{ok, Parsed} = arizona_tpl_parse:parse_exprs(Tokens, Macros),
Parsed.

-spec compile(Mod, Fun, Macros) -> Compiled
when Mod :: module(),
Fun :: atom(),
Macros :: macros(),
Compiled :: tree().
compile(Mod, Fun, Macros) ->
arizona_tpl_compile:compile({Mod, Fun, Macros}).
arizona_tpl_parse:parse_exprs(Tokens, Macros).

%% --------------------------------------------------------------------
%% Callback support functions.
Expand Down Expand Up @@ -125,13 +109,6 @@ handle_event(Mod, Event, Payload, Socket) ->
-include("arizona.hrl").
-include_lib("eunit/include/eunit.hrl").

parse_str_test() ->
?assertMatch([
{tag,
#{name := <<"main">>,
directives := #{stateful := true}}
}], render(#{})).

% Start parse_str support.

render(Macros) ->
Expand All @@ -153,7 +130,11 @@ counter(Macros) ->
% End parse_str support.

compile_test() ->
?assertMatch({ok, #{block := _}}, compile(?MODULE, render, #{})).
?assert(is_block(arizona_tpl_compile:compile(?MODULE, render, #{}))).

-endif.
is_block(#{block := _}) ->
true;
is_block(_NonBlock) ->
false.

-endif.
8 changes: 4 additions & 4 deletions src/arizona_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ start(Opts) ->

-spec route(Req) -> Routed
when Req :: cowboy_req:req(),
Routed :: {ok, Req, Env} | {stop, Req},
Routed :: {Req, Env},
Env :: cowboy_middleware:env().
route(Req) ->
#{path := Path} = cowboy_req:match_qs([path], Req),
cowboy_router:execute(Req#{path => Path},
#{dispatch => {persistent_term, ?PERSIST_KEY}}).
{ok, Req, Env} = cowboy_router:execute(Req#{path => Path},
#{dispatch => {persistent_term, ?PERSIST_KEY}}),
{Req, Env}.

%% --------------------------------------------------------------------
%% Internal functions.
Expand Down Expand Up @@ -111,4 +112,3 @@ norm_proto_opts(Opts, Host, Routes, LiveReload) ->
end}]),
persistent_term:put(?PERSIST_KEY, Dispatch),
Opts#{env => #{dispatch => {persistent_term, ?PERSIST_KEY}}}.

124 changes: 115 additions & 9 deletions src/arizona_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Components state.
-ignore_xref([put_assigns/2]).
-export([put_assign/3]).
-ignore_xref([put_assign/3]).
-export([get_assigns/1]).
-ignore_xref([get_assigns/1]).
-export([get_changes/1]).
-export([get_view/1]).
-export([get_id/1]).
-export([get_events/1]).
-export([get_assign/2]).
-ignore_xref([get_assign/2]).
-export([get_assign/3]).
Expand All @@ -38,18 +44,26 @@ Components state.
-opaque t() :: map().
-export_type([t/0]).

-opaque assigns() :: map().
-type assigns() :: map().
-export_type([assigns/0]).

-type changes() :: map().
-export_type([changes/0]).

-type view() :: module().
-export_type([view/0]).

-type events() :: list().
-export_type([events/0]).

%% --------------------------------------------------------------------
%% API functions.
%% --------------------------------------------------------------------

-spec new(Id, View, Assigns) -> Socket
-spec new(Id, View, Assigns) -> t()
when Id :: atom(),
View :: module(),
Assigns :: assigns(),
Socket :: t().
View :: view(),
Assigns :: assigns().
new(Id, View, Assigns) ->
#{
id => Id,
Expand Down Expand Up @@ -81,17 +95,35 @@ put_assign(Key, Value, Socket) ->
}
end.

-spec get_assign(Key, Socket) -> Got
-spec get_assigns(t()) -> assigns().
get_assigns(#{assigns := Assigns}) ->
Assigns.

-spec get_changes(t()) -> changes().
get_changes(#{changes := Changes}) ->
Changes.

-spec get_view(t()) -> view().
get_view(#{view := View}) ->
View.

-spec get_id(t()) -> atom().
get_id(#{id := Id}) ->
Id.

-spec get_events(t()) -> events().
get_events(#{events := Events}) ->
Events.

-spec get_assign(Key, t()) -> Got
when Key :: atom(),
Socket :: t(),
Got :: term().
get_assign(Key, Socket) ->
#{assigns := Assigns} = Socket,
maps:get(Key, Assigns).

-spec get_assign(Key, Socket, Default) -> Got
-spec get_assign(Key, t(), Default) -> Got
when Key :: atom(),
Socket :: t(),
Default :: term(),
Got :: term().
get_assign(Key, Socket, Default) ->
Expand All @@ -113,3 +145,77 @@ prune(Socket) ->
changes => #{}
}.

%% --------------------------------------------------------------------
%% EUnit tests.
%% --------------------------------------------------------------------

-ifdef(TEST).
-compile([export_all, nowarn_export_all]).
-include_lib("eunit/include/eunit.hrl").

render_block_test() ->
?assertEqual(
rendered(), arizona_tpl_render:render_block(block(#{}), #{
title => <<"Arizona">>,
view_count => 0,
decr_btn_text => <<"Decrement">>})).

rendered() ->
[<<"<main arz-id=\"root\"><h1>">>, <<"Arizona">>,
<<"</h1>">>,
[<<"<div arz-id=\"[3]\" id=\"">>, <<"1">>,
<<"\"><span>">>, <<"Count:">>, <<"<b>">>, <<"0">>,
<<"</b></span><br/>">>,
[<<"Increment">>,
<<"<button arz-target=\"[3]\" onclick=\"">>,
<<"incr">>, <<"\" type=\"button\">">>, <<"Increment">>,
<<"</button>">>, <<"Increment">>],
<<"</div>">>],
[<<"<div arz-id=\"[4]\" id=\"">>, <<"2">>,
<<"\"><span>">>, <<"Rev. Counter:">>, <<"<b>">>, <<"0">>,
<<"</b></span><br/>">>,
[<<"Decrement">>,
<<"<button arz-target=\"[4]\" onclick=\"">>,
<<"decr">>, <<"\" type=\"button\">">>, <<"Decrement">>,
<<"</button>">>, <<"Decrement">>],
<<"</div>">>],
<<"</main>">>].

mount_test() ->
{Render, Sockets} = arizona_tpl_render:mount(block(#{}), #{
title => <<"Arizona">>,
view_count => 0,
decr_btn_text => <<"Decrement">>}),
[?assertEqual(rendered(), Render),
?assertMatch(#{[0] :=
#{id := [0],
events := [], view := arizona_tpl_compile,
assigns :=
#{title := <<"Arizona">>, view_count := 0,
decr_btn_text := <<"Decrement">>},
changes := #{}},
[3] :=
#{id := [3],
events := [], view := arizona_tpl_compile,
assigns :=
#{id := <<"1">>, counter_count := 0,
btn_text := <<"Increment">>,
btn_event := <<"incr">>},
changes := #{}},
[4] :=
#{id := [4],
events := [], view := arizona_tpl_compile,
assigns :=
#{id := <<"2">>, label := <<"Rev. Counter:">>,
counter_count := 0, btn_text := <<"Decrement">>,
btn_event := <<"decr">>},
changes := #{}}}, Sockets)].

%% Start block support.

block(Macros) ->
arizona_tpl_compile:compile(arizona_tpl_compile, view, Macros).

%% End block support.

-endif.
1 change: 0 additions & 1 deletion src/arizona_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ init(_Args) ->
[]
end,
{ok, {SupFlags, ChildSpecs}}.

Loading

0 comments on commit 6c9eb16

Please sign in to comment.