Namdak Tonpa 9 years ago
parent
commit
547dfc19da
1 changed files with 33 additions and 0 deletions
  1. 33 0
      src/actions/action_async.erl

+ 33 - 0
src/actions/action_async.erl

@@ -0,0 +1,33 @@
+-module(action_async).
+-author('Maxim Sokhatsky').
+-include_lib("n2o/include/wf.hrl").
+-compile(export_all).
+
+async(Fun) -> async("comet",Fun).
+async(Name, F) ->
+    Pid = case global:whereis_name(Name) of
+        undefined ->
+            Closure = fun(Fun) ->
+                R = global:register_name(Name,self()),
+                case R of
+                    yes -> Fun();
+                    _ -> skip end end,
+            Req = case wf:context() of
+                undefined -> undefined;
+                _ -> ?REQ
+            end,
+            spawn(fun() -> init_context(Req), Closure(F) end);
+        Registered -> Registered end,
+    {ok,Pid}.
+
+flush(Pool) ->
+    Actions = wf_context:actions(),
+    wf:actions([]),
+    wf:send(Pool,{flush,Actions}).
+
+init_context(undefined) -> [];
+init_context(Req) ->
+    Ctx = wf_context:init_context(Req),
+    NewCtx = wf_context:fold(init, Ctx#cx.handlers, Ctx),
+    wf:actions(NewCtx#cx.actions),
+    wf:context(NewCtx).