by Jake Prem
What is a GenServer?
def callback_mode do
:handle_event_function
:state_functions
[:handle_event_function, :state_enter]
[:state_functions, :state_enter]
end
{:next_state, NextState, NewData, Actions[]}
{:next_state, NextState, NewData}
{:keep_state, NewData, Actions[]}
{:keep_state, NewData}
{:keep_state_and_data, Actions[]}
:keep_state_and_data
{:repeat_state, NewData, Actions[]}
{:repeat_state, NewData}
{:repeat_state_and_data, Actions[]}
...
{:stop, Reason, NewData}
{:stop, Reason}
{:stop_and_reply, Reason, NewData, ReplyActions[]}
{:stop_and_reply, Reason, ReplyActions[]}
{:postpone, Boolean}
{:hibernate, Boolean}
# Cancelled on state_change, or {:state_timeout, :infinity}
{:state_timeout, Time, EventContent}
{{:timeout, Name}, Time}
{:timeout, Time}
# --------------------------------
{:reply, From, Reply}
{:next_event, EventType, EventContent}
# GenServer
def handle_call(msg, from, state) do
...
{:reply, reply_msg, state}
end
def handle_cast(msg, state) do
...
{:noreply, state}
end
# GenStatem
def handle_event({:call, from}, msg_content, state, data) do
...
{..., [{:reply, from, reply_msg} | other_actions]}
end
def handle_cast(:cast, msg_content, state, data) do
...
{..., actions}
end
# GenServer
GenServer.start_link(__MODULE__, args, name: :whatever)
# :gen_statem
:gen_statem.start_link({:local, :whatever}, __MODULE__, args, opts)