applyEvent<E extends Event> method

void applyEvent<E extends Event>(
  1. E event
)

Call this method with an event to transition to a new State.

Calls to applyEvent are serialized as a transition must complete before the next transition begins (otherwise the state would be indeterminant).

A queueing mechanism is used to avoid dead locks, as such you can call applyEvent even whilst in the middle of a call to applyEvent.

Events MUST be handled asynchronously. If you need to take an action once an event completes you need to use a SideEffect or onEnter.

Throws a UnknownStateException if the event results in a transition to a State that hasn't been registered.

Throws an InvalidTransitionException if the event is applied when we are in a state that doesn't support the given event. When in production mode we supress this exception to make the FSM more forgiving.

Implementation

void applyEvent<E extends Event>(E event) {
  final qe = _QueuedEvent(event);
  log('FSM queuing $event');
  _eventQueue.add(qe);

  /// process the event on a microtask.
  Future.delayed(Duration.zero, _dispatch);
}