MachineState constructor

MachineState(
  1. MachineStateKey key,
  2. InitialMachine initialMachine, {
  3. required MachineDoneHandler onMachineDone,
  4. bool isMachineDone(
    1. Transition
    )?,
  5. MachineDisposedHandler? onMachineDisposed,
})

Constructs a machine state, identified by key.

When this state is entered, a nested state machine that is produced by initialMachine will be started. By default any messages dispatched to this state will forwarded to the nested state machine for processing

No transitions from this state will occur until the nested state machine reaches a completion state. By default, any final state is considered a completion state, but non-final states can also be completion states by providing an isMachineDone callback. This function will be called for each transition to a non-final state in the nested machine, and if true is returned, the nested state machine will be considered to have completed.

If a onMachineDisposed callback is provided, it will be called if the nested state machine is disposed, and in a similar manner to onMachineDone, the return value indicates which state to transition to now that the nested machine is disposed. Note that this will typically only be called if initialMachine returns an existing state machine, and that machine is disposed 'out of band' by the application. The machine state carries a state data value of MachineTreeStateData. This value can be obtained in the same ways as other state data, for example using CurrentState.dataValue.

A machine state is always a leaf state. It can be declared as a child state, however all messages will be handled by the machine state until the nested state machine has completed, and as such the parent state will not recieve any unhandled messages from the child machine state.

Implementation

/// The machine state carries a state data value of [MachineTreeStateData].
/// This value can be obtained in the same ways as other state data, for
/// example using [CurrentState.dataValue].
///
/// A machine state is always a leaf state. It can be declared as a child
/// state, however all messages will be handled by the machine state until
/// the nested state machine has completed, and as such the parent state will
/// not recieve any unhandled messages from the child machine state.
///
factory MachineState(
  MachineStateKey key,
  InitialMachine initialMachine, {
  required MachineDoneHandler onMachineDone,
  bool Function(Transition)? isMachineDone,
  MachineDisposedHandler? onMachineDisposed,
}) =>
    MachineState._((parent) {
      return LeafNodeInfo(
        key,
        (_) => MachineTreeState(
          initialMachine,
          (currentMachineState) =>
              (msgCtx) => onMachineDone(msgCtx, currentMachineState),
          null, // Logger
          isMachineDone,
          onMachineDisposed,
        ),
        parent: parent,
        isFinalState: false,
      );
    });