12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- -module(active_events).
- -behaviour(gen_event).
- -export([
- start_link/0,
- subscribe_onload/1,
- subscribe_onnew/1,
- notify_reload/1,
- subscribe/2
- ]).
- -export([
- init/1,
- handle_event/2,
- handle_call/2,
- handle_info/2,
- terminate/2,
- code_change/3
- ]).
- -type mf() :: {M :: module(), F :: atom()}.
- -spec subscribe_onload(Function :: mf()) -> ok.
- -spec subscribe_onnew(Function :: mf()) -> ok.
- -spec subscribe(Event :: reloaded | loaded_new, Function :: mf()) -> ok.
- -record(event_state, {
- event :: reloaded | loaded_new,
- function :: mf()
- }).
- start_link() ->
- gen_event:start_link({local, ?MODULE}).
- subscribe_onload(Function) ->
- subscribe(reloaded, Function).
- subscribe_onnew(Function) ->
- subscribe(loaded_new, Function).
- subscribe(Event, Function) ->
- ok = gen_event:add_sup_handler(?MODULE, {?MODULE, Event}, [Event, Function]).
- notify_reload(Event) ->
- gen_event:notify(?MODULE, Event).
- init([Event, Function]) ->
- {ok, #event_state{event = Event, function = Function}}.
- handle_call(_Request, _State) ->
- erlang:error(not_implemented).
- handle_info(_Info, _State) ->
- erlang:error(not_implemented).
- terminate(_Args, _State) ->
- ok.
- code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
- handle_event({Event, Module}, State = #event_state{event = Event, function = {Mod, Fun}}) ->
- erlang:apply(Mod, Fun, [[Module]]),
- {ok, State};
- handle_event(_, State) -> {ok, State}.
|