Skip to content
Permalink
Browse files
Refactor config listening into custom behaviour
  • Loading branch information
kocolosk committed Jan 18, 2013
1 parent bc181f1 commit d221cb48603c2a9ad6720dbb219ef40f8f90a771
Showing 4 changed files with 42 additions and 64 deletions.
@@ -21,7 +21,7 @@

-export([start_link/1, stop/0]).
-export([all/0, get/1, get/2, get/3, set/3, set/4, delete/2, delete/3]).
-export([register/1, register/2]).
-export([listen_for_changes/1]).
-export([parse_ini_file/1]).

-export([init/1, terminate/2, code_change/3]).
@@ -80,13 +80,8 @@ delete(Section, Key, Persist) when is_binary(Section) and is_binary(Key) ->
delete(Section, Key, Persist) ->
gen_server:call(?MODULE, {delete, Section, Key, Persist}).


register(Fun) ->
?MODULE:register(Fun, self()).

register(Fun, Pid) ->
config_event:register(Fun, Pid).

listen_for_changes(CallbackModule) ->
config_listener:start(CallbackModule).

init(IniFiles) ->
ets:new(?MODULE, [named_table, set, protected]),

This file was deleted.

@@ -0,0 +1,38 @@
-module(config_listener).

-behaviour(gen_event).

%% Public interface
-export([start/1]).

-export([behaviour_info/1]).

%% Required gen_event interface
-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2,
code_change/3]).

behaviour_info(callbacks) ->
[{handle_config_change/4}];
behaviour_info(_) ->
undefined.

start(Module) ->
gen_event:add_sup_handler(config_event, {?MODULE, Module}, [Module]).

init(Module) ->
{ok, Module}.

handle_event({config_change, Sec, Key, Value, Persist}, Module) ->
Module:handle_config_change(Sec, Key, Value, Persist).

handle_call(_Request, Module) ->
{ok, ignored, St}.

handle_info(_Info, Module) ->
{ok, St}.

terminate(_Reason, Module) ->
ok.

code_change(_OldVsn, Module, _Extra) ->
{ok, St}.
@@ -40,7 +40,7 @@ init(IniFiles) ->
[config]
},
{config_event,
{config_event, start_link, []},
{gen_event, start_link, [{local, config_event}]},
permanent,
5000,
worker,

0 comments on commit d221cb4

Please sign in to comment.