You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2014/08/01 11:10:02 UTC

[06/35] git commit: Add a cast API for couch_event_listener

Add a cast API for couch_event_listener


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/commit/320cf0c6
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/tree/320cf0c6
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/diff/320cf0c6

Branch: refs/heads/windsor-merge
Commit: 320cf0c62793fd73af8789d2dc48ac33fcafd6f3
Parents: 7c728ff
Author: Paul J. Davis <pa...@gmail.com>
Authored: Tue Apr 23 15:40:26 2013 -0500
Committer: Robert Newson <rn...@apache.org>
Committed: Wed Jul 30 17:35:58 2014 +0100

----------------------------------------------------------------------
 src/couch_event_listener.erl | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/320cf0c6/src/couch_event_listener.erl
----------------------------------------------------------------------
diff --git a/src/couch_event_listener.erl b/src/couch_event_listener.erl
index 7baca46..042500f 100644
--- a/src/couch_event_listener.erl
+++ b/src/couch_event_listener.erl
@@ -18,7 +18,8 @@
     start/4,
     start_link/3,
     start_link/4,
-    enter_loop/3
+    enter_loop/3,
+    cast/2
 ]).
 
 -export([
@@ -41,6 +42,7 @@ behaviour_info(callbacks) ->
     [
         {init,1},
         {terminate/2},
+        {handle_cast/2},
         {handle_event/2},
         {handle_info/2}
     ];
@@ -79,6 +81,11 @@ enter_loop(Module, State, Options) ->
     ?MODULE:loop(#st{module=Module, state=State}, infinity).
 
 
+cast(Pid, Message) ->
+    Pid ! {'$couch_event_cast', Message},
+    ok.
+
+
 do_init(Module, Arg, Options) ->
     ok = maybe_name_process(Options),
     ok = register_listeners(Options),
@@ -96,6 +103,8 @@ loop(St, Timeout) ->
     receive
         {'$couch_event', DbName, Event} ->
             do_event(St, DbName, Event);
+        {'$couch_event_cast', Message} ->
+            do_cast(St, Message);
         Else ->
             do_info(St, Else)
     after Timeout ->
@@ -140,6 +149,19 @@ do_event(#st{module=Module, state=State}=St, DbName, Event) ->
     end.
 
 
+do_cast(#st{module=Module, state=State}=St, Message) ->
+    case (catch Module:handle_cast(Message, State)) of
+        {ok, NewState} ->
+            ?MODULE:loop(St#st{state=NewState}, infinity);
+        {ok, NewState, Timeout} when is_integer(Timeout), Timeout >= 0 ->
+            ?MODULE:loop(St#st{state=NewState}, Timeout);
+        {stop, Reason, NewState} ->
+            do_terminate(Reason, St#st{state=NewState});
+        Else ->
+            erlang:error(Else)
+    end.
+
+
 do_info(#st{module=Module, state=State}=St, Message) ->
     case (catch Module:handle_info(Message, State)) of
         {ok, NewState} ->