Skip to content

Instantly share code, notes, and snippets.

@eproxus
Last active November 13, 2019 15:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eproxus/28fd864e2ba607d13ed4f5abc89ca27a to your computer and use it in GitHub Desktop.
Save eproxus/28fd864e2ba607d13ed4f5abc89ca27a to your computer and use it in GitHub Desktop.
Erlang Behaviour Templates
-module().
-behavior(gen_event).
% Callbacks
-export([init/1]).
-export([handle_event/2]).
-export([handle_call/2]).
-export([handle_info/2]).
-export([terminate/2]).
%--- Callbacks -----------------------------------------------------------------
init(_Args) -> {ok, undefined}.
handle_event(Event, _State) -> error({unknown_event, Event}).
handle_call(Request, _State) -> error({unknown_request, Request}).
handle_info(Info, _State) -> error({unknown_info, Info}).
terminate(_Arg, _State) -> undefined.
-module().
-behavior(gen_server).
% API
-export([start_link/0]).
% Callbacks
-export([init/1]).
-export([handle_call/3]).
-export([handle_cast/2]).
-export([handle_info/2]).
%--- API -----------------------------------------------------------------------
start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, undefined, []).
%--- Callbacks -----------------------------------------------------------------
init(undefined) -> {ok, undefined}.
handle_call(Request, From, _State) -> error({unknown_request, Request, From}).
handle_cast(Request, _State) -> error({unknown_cast, Request}).
handle_info(Info, _State) -> error({unknown_info, Info}).
-module().
-behavior(gen_statem).
% API
-export([start_link/0]).
% Callbacks
-export([init/1]).
-export([callback_mode/0]).
-export([handle_event/4]).
%--- API -----------------------------------------------------------------------
start_link() -> gen_statem:start_link({local, ?MODULE}, ?MODULE, undefined, []).
%--- Callbacks -----------------------------------------------------------------
init(undefined) -> {ok, state, data}.
callback_mode() -> handle_event_function.
handle_event({call, _From}, EventData, State, _Data) ->
error({unhandled_call, EventData, State});
handle_event(Event, EventData, State, _Data) ->
error({unhandled_event, Event, EventData, State}).
-module().
-behaviour(supervisor).
% API
-export([start_link/0]).
% Callbacks
-export([init/1]).
%--- API -----------------------------------------------------------------------
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%--- Callbacks -----------------------------------------------------------------
init([]) ->
{ok, {#{}, []}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment