refactor(hook): Add emqx_hooks:put to hide already_exists error

This also fixes dialyzer warnings in emqx_mod_presence
This commit is contained in:
Zaiming Shi 2020-11-06 15:02:16 +01:00
parent 5793ef6640
commit ab5599b3ec
2 changed files with 11 additions and 2 deletions

View File

@ -31,6 +31,7 @@
-export([ add/2 -export([ add/2
, add/3 , add/3
, add/4 , add/4
, put/2
, del/2 , del/2
, run/2 , run/2
, run_fold/3 , run_fold/3
@ -111,6 +112,14 @@ add(HookPoint, Action, Priority) when is_integer(Priority) ->
add(HookPoint, Action, Filter, Priority) when is_integer(Priority) -> add(HookPoint, Action, Filter, Priority) when is_integer(Priority) ->
add(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). add(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}).
%% @doc Like add/2, it register a callback, discard 'already_exists' error.
-spec(put(hookpoint(), action() | #callback{}) -> ok).
put(HookPoint, Callback) ->
case add(HookPoint, Callback) of
ok -> ok;
{error, already_exists} -> ok
end.
%% @doc Unregister a callback. %% @doc Unregister a callback.
-spec(del(hookpoint(), function() | {module(), atom()}) -> ok). -spec(del(hookpoint(), function() | {module(), atom()}) -> ok).
del(HookPoint, Action) -> del(HookPoint, Action) ->

View File

@ -38,8 +38,8 @@
-endif. -endif.
load(Env) -> load(Env) ->
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Env]}), emqx_hooks:put('client.connected', {?MODULE, on_client_connected, [Env]}),
emqx_hooks:add('client.disconnected', {?MODULE, on_client_disconnected, [Env]}). emqx_hooks:put('client.disconnected', {?MODULE, on_client_disconnected, [Env]}).
unload(_Env) -> unload(_Env) ->
emqx_hooks:del('client.connected', {?MODULE, on_client_connected}), emqx_hooks:del('client.connected', {?MODULE, on_client_connected}),