isInState<S extends State> method

Future<bool> isInState<S extends State>()

Returns true if the StateMachine is in the given state.

Calling isInState will wait for the StateMachine to complete before checking the state.

For a nested State the machine is said to be in the current leaf State plus any parent state.

  machine.isInState<Hard>()

When using a coregion then you can be in multiple leaf states concurrently.


final machine = StateMachine.create((g) => g
   ..initialState(Solid())
    ..state<Solid>((b) => b
      ..state<Soft>((b) => b)
      ..state<Hard>((b) => b)));

If the above machine is in the leaf state 'Soft' then it is said to also be in the parent state 'Solid'. If a StateMachine has 'n' levels of nested state then it can be in upto 'n' States at any given time.

For a StateMachine.coregion() the machine is said to be in each coregion simultaneously. Coregions can combine with nested states so that a StateMachine can be in all of the nested states for multiple coregions. So if a machine has two coregions and each coregion has 'n' nested states then a StateMachine could be in 2 * 'n' states plus any parents of each of the coregions.

If S is not a known State then an UnknownStateException is thrown.

Implementation

Future<bool> isInState<S extends State>() async {
  await complete;
  if (_stateOfMind.isInState(S)) {
    return true;
  }

  /// climb the tree of nested states.

  final def = _graph.findStateDefinition(S);
  if (def == null) {
    throw UnknownStateException('The state $S has not been registered');
  }
  var parent = def.parent!;
  while (parent.stateType != VirtualRoot) {
    if (parent.stateType == S) {
      return true;
    }

    parent = parent.parent!;
  }
  return false;
}