Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2016 09:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Deserialized gen_server pattern
-export([init/1, handle_call/3, terminate/2]).
-export([hello/2, handle_hello/3]).
-export([world/1, handle_world/3]).
start_link() ->
gen_server:start_link(?MODULE, none, []).
init(none) ->
{ok, #{}}.
%% Generic call. API function provides a handler.
deser_call(Server, Function, Arg) ->
gen_server:call(Server, {'$deser_rpc', Function, Arg}).
handle_call({'$deser_rpc', Function, Arg}, From, State) ->
?MODULE:Function(Arg, From, State).
terminate(_, _) ->
hello(Server, Who) ->
deser_call(Server, handle_hello, Who).
handle_hello(Who, _From, #{} = State) ->
{reply, {hello, Who}, State#{hello => Who}}.
world(Server) ->
deser_call(Server, handle_world, none).
handle_world(none, _From, #{hello := Who} = State) ->
{reply, {Who, nearby}, State};
handle_world(none, _From, #{} = State) ->
{reply, i_am_so_lonely, State}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment