<commit_before>-module(n2o_nitrogen).
-author('<NAME>').
-include_lib("n2o/include/wf.hrl").
-compile(export_all).

% Nitrogen pickle handler

info({init,Rest},Req,State) ->
    Module = State#cx.module,
    InitActions = case Rest of
         <<>> -> Elements = try Module:main() catch X:Y -> wf:error_page(X,Y) end,
                 wf_render:render(Elements),
                 [];
          Binary -> Pid = wf:depickle(Binary),
                    X = Pid ! {'N2O',self()},
                    receive_actions(Req) end,
    UserCx = try Module:event(init) catch C:E -> wf:error_page(C,E) end,
    Actions = render_actions(wf:actions()),
    self() ! {init_reply,wf:json([{eval,iolist_to_binary([InitActions,Actions])}])},
    {cont,Rest,Req,wf:context(State,?MODULE,UserCx)};


info({text,Message},Req,State) ->   info(Message,Req,State);
info({binary,Message},Req,State) -> info(binary_to_term(Message,[safe]),Req,State);

info({pickle,_,_,_}=Event, Req, State) ->
    wf:actions([]),
    wf:info(?MODULE,"N2O Message: ~p\n\r",[Event]),
    Result = try html_events(Event,State) catch E:R -> wf:info(?MODULE,"Catch: ~p:~p~n~p", wf:stack(E, R)), <<>> end,
    {reply,Result,Req,State};

info({flush,Actions}, Req, State) ->
    wf:actions([]),
    wf:info(?MODULE,"Flush Message: ~p",[Actions]),
    {reply, wf:json([{eval,iolist_to_binary(render_actions(Actions))}]), Req, State};

info({direct,Message}, Req, State) ->
    wf:actions([]),
    Module = State#cx.module,
    Term = try Module:event(Message) catch E:R -> wf:info(?MODULE,"Catch: ~p:~p~n~p", wf:stack(E, R)), <<>> end,
    wf:info(?MODULE,"Direct: ~p Result: ~p",[Message,Term]),
    {reply,wf:json([{eval,iolist_to_binary(render_actions(wf:actions()))}]),Req,State};

info(Message,Req,State) -> {unknown,Message,Req,State}.

% double render: actions could generate actions

render_actions(Actions) ->
    wf:actions([]),
    First  = wf:render(Actions),
    Second = wf:render(wf:actions()),
    wf:actions([]),
    [First,Second].

% N2O events

html_events({pickle,Source,Pickled,Linked}, State) ->
    Ev = wf:depickle(Pickled),
    wf:info(?MODULE,"Depickled: ~p",[Ev]),
    case Ev of
         #ev{} -> render_ev(Ev,Source,Linked,State);
         CustomEnvelop -> wf:error("Only #ev{} events for now: ~p",[CustomEnvelop]) end,
    wf:json([{eval,iolist_to_binary(render_actions(wf:actions()))}]).

render_ev(#ev{module=M,name=F,msg=P,trigger=T},Source,Linked,State) ->
    case F of
         api_event -> M:F(P,Linked,State);
         event -> lists:map(fun({K,V})-> put(K,V) end,Linked), M:F(P);
         UserCustomEvent -> M:F(P,T,State) end.

receive_actions(Req) ->
    receive <commit_msg>remove logging in n2o_nitrogen<commit_after>-module(n2o_nitrogen).
-author('<NAME>').
-include_lib("n2o/include/wf.hrl").
-compile(export_all).

% Nitrogen pickle handler

info({init,Rest},Req,State) ->
    Module = State#cx.module,
    InitActions = case Rest of
         <<>> -> Elements = try Module:main() catch X:Y -> wf:error_page(X,Y) end,
                 wf_render:render(Elements),
                 [];
          Binary -> Pid = wf:depickle(Binary),
                    X = Pid ! {'N2O',self()},
                    receive_actions(Req) end,
    UserCx = try Module:event(init) catch C:E -> wf:error_page(C,E) end,
    Actions = render_actions(wf:actions()),
    self() ! {init_reply,wf:json([{eval,iolist_to_binary([InitActions,Actions])}])},
    {cont,Rest,Req,wf:context(State,?MODULE,UserCx)};


info({text,Message},Req,State) ->   info(Message,Req,State);
info({binary,Message},Req,State) -> info(binary_to_term(Message,[safe]),Req,State);

info({pickle,_,_,_}=Event, Req, State) ->
    wf:actions([]),
    %wf:info(?MODULE,"N2O Message: ~p\n\r",[Event]),
    Result = try html_events(Event,State) catch E:R -> wf:info(?MODULE,"Catch: ~p:~p~n~p", wf:stack(E, R)), <<>> end,
    {reply,Result,Req,State};

info({flush,Actions}, Req, State) ->
    wf:actions([]),
    wf:info(?MODULE,"Flush Message: ~p",[Actions]),
    {reply, wf:json([{eval,iolist_to_binary(render_actions(Actions))}]), Req, State};

info({direct,Message}, Req, State) ->
    wf:actions([]),
    Module = State#cx.module,
    Term = try Module:event(Message) catch E:R -> wf:info(?MODULE,"Catch: ~p:~p~n~p", wf:stack(E, R)), <<>> end,
    wf:info(?MODULE,"Direct: ~p Result: ~p",[Message,Term]),
    {reply,wf:json([{eval,iolist_to_binary(render_actions(wf:actions()))}]),Req,State};

info(Message,Req,State) -> {unknown,Message,Req,State}.

% double render: actions could generate actions

render_actions(Actions) ->
    wf:actions([]),
    First  = wf:render(Actions),
    Second = wf:render(wf:actions()),
    wf:actions([]),
    [First,Second].

% N2O events

html_events({pickle,Source,Pickled,Linked}, State) ->
    Ev = wf:depickle(Pickled),
    %wf:info(?MODULE,"Depickled: ~p",[Ev]),
    case Ev of
         #ev{} -> render_ev(Ev,Source,Linked,State);
         CustomEnvelop -> wf:error("Only #ev{} events for now: ~p",[CustomEnvelop]) end,
    wf:json([{eval,iolist_to_binary(render_actions(wf:actions()))}]).

render_ev(#ev{module=M,name=F,msg=P,trigger=T},Source,Linked,State) ->
    case F of
         api_event -> M:F(P,Linked,State);
         event -> lists:map(fun({K,V})-> put(K,V) end,Linked), M:F(P);
         UserCustomEvent -> M:F(P,T,State) end.

receive_actions(Req) ->
    receive 
{'fraction_non_alphanumeric': 0.2065359477124183, 'fraction_numerical': 0.002054154995331466, 'mean_word_length': 4.261296660117878, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7294518', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1953, 'n_words': 303}