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:
parent
5793ef6640
commit
ab5599b3ec
|
@ -31,6 +31,7 @@
|
|||
-export([ add/2
|
||||
, add/3
|
||||
, add/4
|
||||
, put/2
|
||||
, del/2
|
||||
, run/2
|
||||
, 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, #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.
|
||||
-spec(del(hookpoint(), function() | {module(), atom()}) -> ok).
|
||||
del(HookPoint, Action) ->
|
||||
|
|
|
@ -38,8 +38,8 @@
|
|||
-endif.
|
||||
|
||||
load(Env) ->
|
||||
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Env]}),
|
||||
emqx_hooks:add('client.disconnected', {?MODULE, on_client_disconnected, [Env]}).
|
||||
emqx_hooks:put('client.connected', {?MODULE, on_client_connected, [Env]}),
|
||||
emqx_hooks:put('client.disconnected', {?MODULE, on_client_disconnected, [Env]}).
|
||||
|
||||
unload(_Env) ->
|
||||
emqx_hooks:del('client.connected', {?MODULE, on_client_connected}),
|
||||
|
|
Loading…
Reference in New Issue