Change state automatically in gen_statem

Let's imagine that I have the following scenario: I have three states in a state machine coded in gen_statem with state_functions. From the state A to B I do need a trigger but I want that from state B to C I do not need, because state B is going to execute some code and then pass the machine state to state C (named Idle), to wait others inputs. Check the following picture that illustrates the state machine:

State Machine

Is it possible to do this? By now I only was able to pass state to state with a trigger.

Upvotes: 2

Views: 198

Answers (1)

Pascal
Pascal

Reputation: 14042

I never used it, but I think that you can achieve this whith action.

In state 'A', when you receive command you can define in the return value of the callback both the next state 'B' and an action that may be an event that will be inserted on top of event queue so it will be triggered as soon as you enter in 'B':

state_A({call,From},command,Data) ->
    NewData = do_something(Data),
    {next_state,state_B,NewData,[{next_event,cast,go_to_C},{reply,From,done}]}.

Then in state 'B' you will receive immediately the event go_to_C, and you can handle it with its own call back:

state_B(cast,go_to_C,Data) ->
    NewData = other_stuff(Data),
    {next_state,state_C,NewData}.

Upvotes: 1

Related Questions