Events

For example when receiving an 'UserLoggedIn' event it is likely that you need to fetch some additional data. To facilitate this you can pass the user id in the event.Events are objects passed into the state machine with the intent of causing the FSM to transition to a new state.

You will often model real world events 'Turn Light on' but equally you will create many events that reflect internal events within your software or even somewhat abstract events that simply help your state machine to arrive in the correct state.

Some events won't result in a transition to a new state as the likes of a guard condition or a Join may not be satisfied.

To send an event to the state machine you use:

statemachine.applyEvent(SomeEvent());

The call to 'applyEvent' is asynchronous however you MUST NEVER 'await' a call to 'applyEvent' as you risk deadlocking your state machine.

Event Data

You will often find it useful to pass information along with your event. You might have defined a sideEffect that uses data from your event.

class UserLoggedIn extends Event
{
 int userId;
 UserLoggedIn(this.userId);
}

stateMachine.applyEvent(UserLoggedIn(userid));

Within your statemachine you can then define an sideEffect that takes that user id and fetches the user's credit score.

..state<NoSession>((b) => b
  ..on<UserLoggedIn, HasSession>(sideEffect: (e) => fetchCreditScore(e.userId)) 

Terminology

Phrase

Meaning

Applying an Event

An Event is 'applied' to the StateMachine which may cause a state transition.

Receive an Event

An event may be received but not cause a transition to trigger. This may be because its an invalid event for the current state, guard conditions prevent the transition being triggered or a join condition hasn't been met.

Trigger an Event

An event has been received and a valid transition found. The guard condition has evaluated to true and the transition will be executed. The state machine may not however change state.

State Transition

An event was triggered resulting in a change of State.

Last updated