schedule<M2> method

_MessageActionWithData<M, D> schedule<M2>({
  1. FutureOr<M2 Function()> getMessage(
    1. MessageContext msgCtx,
    2. M msg,
    3. D data
    )?,
  2. M2? message,
  3. Duration duration = Duration.zero,
  4. bool periodic = false,
  5. String? label,
})

Schedules a message to be processed by the state machine while a message is being handled.

If getMessage is provided, the function will be evaluated to produce a message function which will be called on each scheduling interval to produce the message to post. When getMessage is called, it is provided a MessageContext, the message that is being handled, and the current data for the state. If getMessage is not provided, then message must be.

The scheduling will be performed using TransitionContext.schedule. Refer to that method for further details of scheduling semantics.

This action can be labeled when formatting a state tree by providing a label.

Implementation

_MessageActionWithData<M, D> schedule<M2>({
  FutureOr<M2 Function()> Function(MessageContext msgCtx, M msg, D data)? getMessage,
  M2? message,
  Duration duration = Duration.zero,
  bool periodic = false,
  String? label,
}) {
  if (getMessage == null && message == null) {
    throw ArgumentError('getMessage or message must be provided');
  } else if (getMessage != null && message != null) {
    throw ArgumentError('One of getMessage or message must be provided');
  }
  var _getMessage = getMessage;
  _getMessage ??= (_, __, ___) => () => message!;

  return _MessageActionWithData<M, D>._(
    _ActionType.schedule,
    (msgCtx, msg, data) => _getMessage!(msgCtx, msg, data).bind((scheduleMsg) {
      _logger.fine(() =>
          "State '$_forState' is scheduling message of type $M2: Duration $duration, Periodic: $periodic");
      msgCtx.schedule(
        scheduleMsg as Object Function(),
        duration: duration,
        periodic: periodic,
      );
    }),
    TypeLiteral<M2>().type,
    label,
  );
}