ixfx
    Preparing search index...
    • Drives a state machine.

      Uses a 'handlers' structure to determine when to change state and actions to take.

      The structure is a set of logical conditions: if we're in this state, then move to this other state etc.

      const handlers = [
      {
      // If we're in the 'sleeping' state, move to next state
      if: 'sleeping',
      then: { next: true }
      },
      {
      // If we're in the 'waking' state, randomly either go to 'resting' or 'sleeping' state
      if: 'waking',
      then: [
      () => {
      if (Math.random() > 0.5) {
      return { next: 'resting' }
      } else {
      return { next: 'sleeping' }
      }
      }
      ]
      }
      ];

      Set up the driver, and call run() when you want to get the machine to change state or take action:

      const driver = await StateMachine.driver(states, handlers);
      setInterval(async () => {
      await driver.run(); // Note use of 'await' again
      }, 1000);

      Essentially, the 'handlers' structure gets run through each time run() is called.

      Defaults to selecting the highest-ranked result to determine what to do next.

      Type Parameters

      Parameters

      Returns Promise<DriverRunner<V>>