xstate

by bejnar

HTML

<script src="https://unpkg.com/xstate@4/dist/xstate.js"></script>

JavaScript

const { createMachine, actions, interpret } = XState; // global variable: window.XState

const lightMachine = createMachine({
  // Machine identifier
  id: 'light',

  // Initial state
  initial: 'green',

  // Local context for entire machine
  context: {
    elapsed: 0,
    direction: 'east'
  },

  // State definitions
  states: {
    green: {
      /* ... */
    },
    yellow: {
      /* ... */
    },
    red: {
      /* ... */
    }
  }
});

const lightService = interpret(lightMachine);