Skip to content

Instantly share code, notes, and snippets.

@jadlr jadlr/pushbutton.ex
Last active Jan 31, 2019

Embed
What would you like to do?
`gen_statem` example rewritten to elixir (http://erlang.org/doc/design_principles/statem.html)
defmodule PushButton 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: :state_functions
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 off({:call, from}, :push, data), do: {:next_state, :on, data + 1, [{:reply, from, :on}]}
def off(event, content, data), do: handle_event(event, content, data)
def on({:call, from}, :push, data), do: {:next_state, :off, data + 1, [{:reply, from, :off}]}
def on(event, content, data), do: handle_event(event, content, data)
def handle_event({:call, from}, :get_count, 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.