when method

StatesTransition? when({
  1. required String at,
  2. required String to,
  3. required String on,
  4. StatesTransitionHandler? handler,
})
override

Add a valid link between two states. The state machine can then move between

@param fromState State you want to move from. @param toState State you want to move to. @param action Action that when performed will move from the from state to the to state. @param handler Optional method that gets called when moving between these two states. @return true if link was added, false if it was not.

Implementation

StatesTransition? when({
  required String at,
  required String to,
  required String on,
  StatesTransitionHandler? handler
}) {
  // print('< States -> when: ${at} | ${to} | ${on}');
  if (locked) return null;

  StatesMeta? metaFrom;
  StatesMeta? metaTo;

  /// can't have duplicate actions
  for (StatesTransition transitions in _transitions) {
    final actionAlreadyRegistered =
      transitions.at?.name == at &&
      transitions.to?.name == to &&
      transitions.handlers.contains(handler) &&
      transitions.action == on;

    if (actionAlreadyRegistered) return null;
  }

  metaFrom = _findStateMetaByName(at);
  if (metaFrom == null) {
    metaFrom = add(at);
  }

  metaTo = _findStateMetaByName(to);
  if (metaTo == null) {
    metaTo = add(to);
  }

  final st = StatesTransition(
      metaFrom!,
      metaTo!,
      on,
      handler
  );
  _transitions.add(st);

  return st;
}