mode :handle_event_function # Client def start_link(), do: GenStateMachine.start_link(__MODULE__, {:locked, nil}) def push(turnstile), do: GenStateMachine.call(turnstile, :push) def coin(turnstile), do: GenStateMachine.call(turnstile, :coin) def report(turnstile), do: GenStateMachine.cast(turnstile, :report) # Server def handle_event({:call, from}, :coin, :locked, data), do: {:next_state, :unlocked, data, [{:reply, from, :unlock}]} def handle_event({:call, from}, :push, :locked, _data), do: {:keep_state_and_data, [{:reply, from, :noop}]} def handle_event(:cast, :report, :locked, _data), do: IO.puts("I am currently locked!") && :keep_state_and_data def handle_event({:call, from}, :coin, :unlocked, _data), do: {:keep_state_and_data, [{:reply, from, :noop}]} def handle_event({:call, from}, :push, :unlocked, data), do: {:next_state, :locked, data, [{:reply, from, :lock}]} def handle_event(:cast, :report, :unlocked, _data), do: IO.puts("I am currently unlocked!") && :keep_state_and_data end