findTriggerableTransition<E extends Event> method

Future<TransitionDefinition<Event>?> findTriggerableTransition<E extends Event>(
  1. Type fromState,
  2. E event
)

Returns the first transition that can be triggered for the given event from the given fromState.

When considering each event we must evaulate the guard condition to determine if the transition is valid.

If no triggerable event can be found for the fromState then a NoOpTransitionDefinition is returned indicating that no transition will occur under the current conditions.

If no matching event can be found for the fromState then an InvalidTransitionException is thrown.

When searching for an event we have to do a recursive search (starting at the fromState) up the tree of nested states as any events on an ancestor State also apply to the child fromState.

Implementation

Future<TransitionDefinition?> findTriggerableTransition<E extends Event>(
    Type fromState, E event) async {
  TransitionDefinition transitionDefinition;

  if (!_hasTransition(fromState, event)) {
    return null;
  }

  /// does the current state definition have a transition
  ///  that will fire for the give event.
  transitionDefinition = await _evaluateTransitions(event);

  // If [fromState] doesn't have a transitionDefintion that can be triggered
  // then we search the parents.
  var parent = this.parent;
  while (transitionDefinition is NoOpTransitionDefinition &&
      parent!.stateType != VirtualRoot) {
    transitionDefinition = await parent._evaluateTransitions(event);
    // as FutureOr<NoOpTransitionDefinition<State, Event>>);
    parent = parent.parent;
  }

  return transitionDefinition;
}