schedule<M2> method

_MessageAction<M> schedule<M2>({
  1. FutureOr<M2 Function()> getMessage(
    1. MessageContext msgCtx,
    2. M msg
    )?,
  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 and the message that is being handled. 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.

class DoItLater {}
class DoIt {}

DoIt Function() onSchedule(MessageContext, DoItLater) {
  // This function will be called when the schedule elapses to produce a
  // message to post
  return () => DoIt();
}
void onDoIt(MessageContext, DoIt) => print('It was done');

var state1 = StateKey('s1');
var builder = StateTreeBuilder(initialState: state1);

builder.state(state1, (b) {
  // Handle DoItLater message by scheduling a DoIt message to be posted in
  // the future
  b.onMessage<DoItLater>((b) => b.stay(action: b.act.schedule<DoIt>(
    onSchedule,
    duration: Duration(milliseconds: 10))));
  // Handle the DoIt message that was scheduled
  b.onMessage<DoIt>((b) => b.stay(action: b.act.run(onDoIt)));
});

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

Implementation

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

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