couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [09/35] git commit: Add an API for the common case
Date Fri, 01 Aug 2014 09:10:05 GMT
Add an API for the common case

Most of our current event listeners are single functions that don't
carry much state. Rather than force us to write a behavior module for
every one of these cases we can use this link_listener/4,
stop_listener/1 API to handle this common pattern.

Under the covers this just wraps couch_event_listener rather thinly to
give us the single callback export version of listening for events.


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/bf25e9f0
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/tree/bf25e9f0
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/diff/bf25e9f0

Branch: refs/heads/windsor-merge
Commit: bf25e9f0b3fd763b1d8e657ea3ef828747437e15
Parents: 8f63a78
Author: Paul J. Davis <paul.joseph.davis@gmail.com>
Authored: Tue Apr 23 16:37:08 2013 -0500
Committer: Robert Newson <rnewson@apache.org>
Committed: Wed Jul 30 17:36:40 2014 +0100

----------------------------------------------------------------------
 src/couch_event.erl              | 29 +++++++++---
 src/couch_event_listener_mfa.erl | 85 +++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/bf25e9f0/src/couch_event.erl
----------------------------------------------------------------------
diff --git a/src/couch_event.erl b/src/couch_event.erl
index ffee077..fd04824 100644
--- a/src/couch_event.erl
+++ b/src/couch_event.erl
@@ -13,20 +13,39 @@
 -module(couch_event).
 
 -export([
+    notify/2
+]).
+
+-export([
+    link_listener/4,
+    stop_listener/1
+]).
+
+-export([
     register/2,
     register_many/2,
     register_all/1,
     unregister/2,
     unregister_many/2,
-    unregister_all/1,
-    notify/2
+    unregister_all/1
 ]).
 
-
 -define(REGISTRY, couch_event_registry).
 -define(DIST, couch_event_dist).
 
 
+notify(DbName, Event) ->
+    gen_server:cast(?DIST, {DbName, Event}).
+
+
+link_listener(Module, Function, State, Options) ->
+    couch_event_listener_mfa:start_link(Module, Function, State, Options).
+
+
+stop_listener(Pid) ->
+    couch_event_listener_mfa:stop(Pid).
+
+
 register(Pid, DbName) ->
     gen_server:call(?REGISTRY, {register, Pid, [DbName]}).
 
@@ -49,7 +68,3 @@ unregister_many(Pid, DbNames) when is_list(DbNames) ->
 
 unregister_all(Pid) ->
     gen_server:call(?REGISTRY, {unregister, Pid}).
-
-
-notify(DbName, Event) ->
-    gen_server:cast(?DIST, {DbName, Event}).

http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/bf25e9f0/src/couch_event_listener_mfa.erl
----------------------------------------------------------------------
diff --git a/src/couch_event_listener_mfa.erl b/src/couch_event_listener_mfa.erl
new file mode 100644
index 0000000..2231448
--- /dev/null
+++ b/src/couch_event_listener_mfa.erl
@@ -0,0 +1,85 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_event_listener_mfa).
+-behavior(couch_event_listener).
+
+
+-export([
+    start_link/4,
+    stop/1
+]).
+
+-export([
+    init/1,
+    terminate/2,
+    handle_event/3,
+    handle_cast/2,
+    handle_info/2
+]).
+
+
+-record(st, {
+    mod,
+    func,
+    state,
+    parent
+}).
+
+
+start_link(Mod, Func, State, Options) ->
+    Arg = {self(), Mod, Func, State},
+    couch_event_listener:start_link(?MODULE, Arg, Options).
+
+
+stop(Pid) ->
+    couch_event_listener:stop(Pid).
+
+
+init({Parent, Mod, Func, State}) ->
+    erlang:monitor(process, Parent),
+    {ok, #st{
+        mod = Mod,
+        func = Func,
+        state = State,
+        parent = Parent
+    }}.
+
+
+terminate(_Reason, _MFA) ->
+    ok.
+
+
+handle_event(DbName, Event, #st{mod=Mod, func=Func, state=State}=St) ->
+    case (catch Mod:Func(DbName, Event, State)) of
+        {ok, NewSt} ->
+            {ok, St#st{state=NewState}};
+        stop ->
+            {stop, normal, St};
+        Else ->
+            erlang:error(Else)
+    end.
+
+
+handle_cast(shutdown, St) ->
+    {stop, normal, St};
+
+handle_cast(_Msg, St) ->
+    {ok, St}.
+
+
+handle_info({'DOWN', _Ref, process, Parent, _Reason}, #st{parent=Parent}=St) ->
+    {stop, normal, St};
+
+handle_info(_Msg, St) ->
+    {ok, St}.
+


Mime
View raw message