findTransition method

Transition<STATE, ACTION> findTransition(
  1. STATE currentState,
  2. ACTION action
)
inherited

Finds a Transition corresponding to currentState and action from the Graph of the StateMachine. If found, it returns the destination STATE as Valid, and if not found, it returns Invalid.

Implementation

Transition<STATE, ACTION> findTransition(STATE currentState, ACTION action) {
  final stateConfig =
      _graph.transitionPatternMap.entries.firstWhereOrNull((element) {
    if (element.key.matches(currentState)) {
      return element.value.transitionMap.entries
              .firstWhereOrNull((element) => element.key.matches(action)) !=
          null;
    } else {
      return false;
    }
  });
  if (stateConfig == null) {
    return Invalid(currentState, action);
  }

  final transition = stateConfig.value.transitionMap.entries
      .firstWhere((element) => element.key.matches(action))
      .value(currentState, action);

  return Valid<STATE, ACTION>(currentState, action, transition.toState);
}