Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`gen_statem` example rewritten in elixir using only handle_event functions
defmodule PushButtonHandleEvents do
@behaviour :gen_statem
@name :pushbutton_statem
# Client API
def start do
:gen_statem.start({:local,@name}, __MODULE__, [], [])
end
def push do
:gen_statem.call(@name, :push)
end
def get_count do
:gen_statem.call(@name, :get_count)
end
def stop do
:gen_statem.stop(@name)
end
# Callbacks
def callback_mode, do: :handle_event_function
def init([]), do: {:ok, :off, 0}
def terminate(_reason, _state, _data), do: :void
def code_change(_vsn, state, data, _extra), do: {:ok, state, data}
# State functions
def handle_event({:call, from}, :push, :off, data), do: {:next_state, :on, data + 1, [{:reply, from, :on}]}
def handle_event({:call, from}, :push, :on, data), do: {:next_state, :off, data + 1, [{:reply, from, :off}]}
def handle_event({:call, from}, :get_count, _state, data), do: {:keep_state, data, [{:reply, from, data}]}
def handle_event(_event, _content, data), do: {:keep_state, data}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.