Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add a cast API for couch_event_listener
  • Loading branch information
davisp authored and rnewson committed Jul 30, 2014
1 parent 7c728ff commit 320cf0c62793fd73af8789d2dc48ac33fcafd6f3
Showing 1 changed file with 23 additions and 1 deletion.
@@ -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} ->

0 comments on commit 320cf0c

Please sign in to comment.