Skip to content

Commit

Permalink
wf:temp_id() tp use erlang:unique_integer
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed Jul 27, 2015
1 parent 0cbbd46 commit 80a70b4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*~
.*.sw?
crypto_compat.hrl
compat.hrl
.deps_plt
test/
.rebar/
Expand Down
24 changes: 17 additions & 7 deletions crypto_compat.escript → compat.escript
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
main([]) ->
crypto:start(),

Filename = "include/crypto_compat.hrl",
io:format("Generating crypto compatibility...\n"),
Filename = "include/compat.hrl",
io:format("Generating compatibility macros...\n"),
Encrypt = encrypt(),
Decrypt = decrypt(),
Hash = hash(),
Unique = unique(),

io:format("...Using: ~p~n",[Encrypt]),
io:format("...Using: ~p~n",[Decrypt]),
io:format("...Using: ~p~n",[Hash]),
io:format("...?WF_ENCRYPT will use: ~p~n",[Encrypt]),
io:format("...?WF_DECRYPT will use: ~p~n",[Decrypt]),
io:format("...?WF_HASH will use: ~p~n",[Hash]),
io:format("...?WF_UNIQUE will use: ~p~n",[Unique]),

Contents = [
"-define(WF_ENCRYPT(Key, IV, Data), ",Encrypt,").\n",
"-define(WF_DECRYPT(Key, IV, Data), ",Decrypt,").\n",
"-define(WF_HASH(Data), ",Hash,").\n"
"-define(WF_HASH(Data), ",Hash,").\n",
"-define(WF_UNIQUE, ",Unique,").\n"
],

ContentsBin = iolist_to_binary(Contents),
Expand Down Expand Up @@ -46,11 +49,18 @@ decrypt() ->
"crypto:aes_cbc_128_decrypt(Key, IV, Data)"
end.


hash() ->
case erlang:function_exported(crypto, hash, 2) of
true ->
"crypto:hash(sha, Data)";
false ->
"crypto:sha(Data)"
end.

unique() ->
case erlang:function_exported(erlang, unique_integer, 1) of
true ->
"erlang:unique_integer([positive])";
false ->
"begin {_,S,US}=erlang:now(), S*1000,+US end"
end.
2 changes: 1 addition & 1 deletion include/wf.hrl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% vim: sw=4 ts=4 et ft=erlang
-ifndef(wf_inc).
-define(wf_inc, ok).
-include("crypto_compat.hrl").
-include("compat.hrl").
-include("wf_test.hrl").


Expand Down
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{cover_enabled, true}.
{xref_checks, [undefined_function_calls]}.
{pre_hooks,[
{"linux|bsd|darwin|solaris", compile, "./crypto_compat.escript"},
{"win32", compile, "escript.exe crypto_compat.escript"}
{"linux|bsd|darwin|solaris", compile, "./compat.escript"},
{"win32", compile, "escript.exe compat.escript"}
]}.
25 changes: 22 additions & 3 deletions src/lib/wf_render_elements.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,27 @@ render_element(Element) when is_tuple(Element) ->
_Html = call_element_render(transform_element, Module, Element);

false ->
% TODO: Revisit generating an ID for each element, instead
% generating the ID only if an element has actions.
% Otherwise, if an element needs an ID for something
% special (like how the #button element needs an anchor to
% wire the postback to and for validation stuff), let the
% element_render function take care of that itself.
%
% This change will provide a handful of performance
% improvements:
% + Removes having to call temp_id() for every element
% + Removes having to call normalize_id() possibly twice
% for each element
% + Lightens the page size since every element won't have
% an unnecessary 'tempABCXYZ' class.


% If no ID is defined, then use the same
% temp_id() for both the HtmlID and TempID.
% Otherwise, create a new TempID. Update the class
% with either one or both.

% Get the anchor, or create a new one if it's not defined...
Anchor = case Base#elementbase.anchor of
undefined -> normalize_id(temp_id());
Expand Down Expand Up @@ -129,5 +145,8 @@ normalize_id(ID) ->

-spec temp_id() -> string().
temp_id() ->
{_, _, C} = now(),
"temp" ++ integer_to_list(C).
Num = ?WF_UNIQUE, %% For Erlang 18+, is erlang:unique_integer,
%% For Erlang <18, is parts of erlang:now()
%% see compat.escript, and include/compat.hrl for the
%% definition.
"temp" ++ integer_to_list(Num).

0 comments on commit 80a70b4

Please sign in to comment.