schedule<M2> method

MessageActionDescriptor<M, D, C> schedule<M2>({
  1. FutureOr<M2> getMessage(
    1. MessageHandlerContext<M, D, C> ctx
    )?,
  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 MessageHandlerContext. 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 onSchedule(MessageHandlerContext<DoItLater, void, void> ctx) {
  // 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

MessageActionDescriptor<M, D, C> schedule<M2>({
  FutureOr<M2> Function(MessageHandlerContext<M, D, C> ctx)? 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 ?? (_) => message!;
  var info = MessageActionInfo(ActionType.schedule, M2, null, label);
  return MessageActionDescriptor<M, D, C>(
    info,
    (ctx) => _getMessage(ctx).bind((msg) {
      _log.fine(() =>
          "State '$_forState' is scheduling message of type $M2 ${periodic ? 'periodic: true' : ''}");
      ctx.messageContext.schedule(
        () => msg as Object,
        duration: duration,
        periodic: periodic,
      );
    }),
  );
}