Skip to content

Instantly share code, notes, and snippets.

@egobrain
Last active August 29, 2015 14:05
Show Gist options
  • Save egobrain/5779fd5284de8a9de2b4 to your computer and use it in GitHub Desktop.
Save egobrain/5779fd5284de8a9de2b4 to your computer and use it in GitHub Desktop.
Thirdparty gproc sub.
-module(pub_sub).
-export([
sub/1,
sub/2,
pub/2
]).
sub(Property) ->
sub(Property, self()).
sub(Property, Pid) ->
Key = {p, l, Property},
Value = undefined,
case gproc_lib:insert_reg(Key, Value, Pid, l) of
false -> throw({gproc_error, badarg});
true -> monitor_pid(Pid)
end.
monitor_pid(Pid) ->
case ets:insert_new(gproc, {{Pid,l}}) of
false -> true;
true ->
gen_server:cast(gproc, {monitor_me,Pid}),
true
end.
pub(Property, Msg) ->
gproc:send({p, l, Property}, Msg).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment