xstate
Simple JavaScript Finite State Machines and Statecharts
Last updated 6 years ago by davidkpiano .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install xstate 
SYNC missed versions from official npm registry.

xstate

Travis npm Statecharts gitter chat

Functional, stateless JavaScript finite state machines and statecharts.

Super quick start

npm i xstate -S
import { Machine } from 'xstate';

const toggleMachine = Machine({
  initial: 'inactive',
  states: {
    inactive: { on: { TOGGLE: 'active' } },
    active: { on: { TOGGLE: 'inactive' } }
  }
});

// Interpret the machine however you want.
// Here's a simple (side-effectful) example:
let currentState = toggleMachine.initialState;

function send(event) {
  currentState = toggleMachine.transition(currentState, event);
  console.log(currentState.value);
}

send('TOGGLE'); // 'active'
send('TOGGLE'); // 'inactive'

📖 Read the documentation!

Visualizer

:new: Preview and simulate your statecharts in the xstate visualizer (beta)!

xstate visualizer

3rd-Party Usage

With sketch.systems, you can now copy-paste your state machine sketches as xstate-compatible JSON!

  1. Create your sketch (example: https://sketch.systems/anon/sketch/new)
  2. Click Export to clipboard...
  3. Select XState JSON

Why?

Statecharts are a formalism for modeling stateful, reactive systems. This is useful for declaratively describing the behavior of your application, from the individual components to the overall application logic.

Read 📽 the slides (🎥 video) or check out these resources for learning about the importance of finite state machines and statecharts in user interfaces:

Installation

  1. npm install xstate --save
  2. import { Machine } from 'xstate';

Finite State Machines

Light Machine
import { Machine } from 'xstate';

const lightMachine = Machine({
  key: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: 'yellow',
      }
    },
    yellow: {
      on: {
        TIMER: 'red',
      }
    },
    red: {
      on: {
        TIMER: 'green',
      }
    }
  }
});

const currentState = 'green';

const nextState = lightMachine
  .transition(currentState, 'TIMER')
  .value;

// => 'yellow'

Hierarchical (Nested) State Machines

Hierarchical Light Machine
import { Machine } from 'xstate';

const pedestrianStates = {
  initial: 'walk',
  states: {
    walk: {
      on: {
        PED_TIMER: 'wait'
      }
    },
    wait: {
      on: {
        PED_TIMER: 'stop'
      }
    },
    stop: {}
  }
};

const lightMachine = Machine({
  key: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: 'yellow'
      }
    },
    yellow: {
      on: {
        TIMER: 'red'
      }
    },
    red: {
      on: {
        TIMER: 'green'
      },
      ...pedestrianStates
    }
  }
});

const currentState = 'yellow';

const nextState = lightMachine
  .transition(currentState, 'TIMER')
  .value;
// => {
//   red: 'walk'
// }

lightMachine
  .transition('red.walk', 'PED_TIMER')
  .value;
// => {
//   red: 'wait'
// }

Object notation for hierarchical states:

// ...
const waitState = lightMachine
  .transition({ red: 'walk' }, 'PED_TIMER')
  .value;

// => { red: 'wait' }

lightMachine
  .transition(waitState, 'PED_TIMER')
  .value;

// => { red: 'stop' }

lightMachine
  .transition({ red: 'stop' }, 'TIMER')
  .value;

// => 'green'

Parallel State Machines

Parallel state machine
const wordMachine = Machine({
  parallel: true,
  states: {
    bold: {
      initial: 'off',
      states: {
        on: {
          on: { TOGGLE_BOLD: 'off' }
        },
        off: {
          on: { TOGGLE_BOLD: 'on' }
        }
      }
    },
    underline: {
      initial: 'off',
      states: {
        on: {
          on: { TOGGLE_UNDERLINE: 'off' }
        },
        off: {
          on: { TOGGLE_UNDERLINE: 'on' }
        }
      }
    },
    italics: {
      initial: 'off',
      states: {
        on: {
          on: { TOGGLE_ITALICS: 'off' }
        },
        off: {
          on: { TOGGLE_ITALICS: 'on' }
        }
      }
    },
    list: {
      initial: 'none',
      states: {
        none: {
          on: { BULLETS: 'bullets', NUMBERS: 'numbers' }
        },
        bullets: {
          on: { NONE: 'none', NUMBERS: 'numbers' }
        },
        numbers: {
          on: { BULLETS: 'bullets', NONE: 'none' }
        }
      }
    }
  }
});

const boldState = wordMachine
  .transition('bold.off', 'TOGGLE_BOLD')
  .value;

// {
//   bold: 'on',
//   italics: 'off',
//   underline: 'off',
//   list: 'none'
// }

const nextState = wordMachine
  .transition({
    bold: 'off',
    italics: 'off',
    underline: 'on',
    list: 'bullets'
  }, 'TOGGLE_ITALICS')
  .value;

// {
//   bold: 'off',
//   italics: 'on',
//   underline: 'on',
//   list: 'bullets'
// }

History States

Machine with history state
const paymentMachine = Machine({
  initial: 'method',
  states: {
    method: {
      initial: 'cash',
      states: {
        cash: { on: { SWITCH_CHECK: 'check' } },
        check: { on: { SWITCH_CASH: 'cash' } },
        hist: { history: true }
      },
      on: { NEXT: 'review' }
    },
    review: {
      on: { PREVIOUS: 'method.hist' }
    }
  }
});

const checkState = paymentMachine
  .transition('method.cash', 'SWITCH_CHECK');

// => State {
//   value: { method: 'check' },
//   history: State { ... }
// }

const reviewState = paymentMachine
  .transition(checkState, 'NEXT');

// => State {
//   value: 'review',
//   history: State { ... }
// }

const previousState = paymentMachine
  .transition(reviewState, 'PREVIOUS')
  .value;

// => { method: 'check' }

Sponsors

Huge thanks to the following companies for sponsoring xstate. You can sponsor further xstate development on OpenCollective.

Interpreters

Current Tags

  • 5.0.0-alpha.6                                ...           alpha (2 years ago)
  • 5.0.0-beta.54                                ...           beta (a year ago)
  • 5.18.2                                ...           latest (2 months ago)

248 Versions

  • 5.18.2                                ...           2 months ago
  • 5.18.1                                ...           2 months ago
  • 5.18.0                                ...           3 months ago
  • 5.17.4                                ...           3 months ago
  • 5.17.3                                ...           3 months ago
  • 5.17.2                                ...           3 months ago
  • 5.17.1                                ...           3 months ago
  • 5.17.0                                ...           4 months ago
  • 5.16.0                                ...           4 months ago
  • 5.15.0                                ...           4 months ago
  • 5.14.0                                ...           5 months ago
  • 5.13.2                                ...           5 months ago
  • 5.13.1                                ...           5 months ago
  • 5.13.0                                ...           6 months ago
  • 5.12.0                                ...           7 months ago
  • 5.11.0                                ...           7 months ago
  • 5.10.0                                ...           7 months ago
  • 5.9.1                                ...           8 months ago
  • 5.9.0                                ...           8 months ago
  • 5.8.2                                ...           9 months ago
  • 5.8.1                                ...           9 months ago
  • 5.8.0                                ...           9 months ago
  • 5.7.1                                ...           9 months ago
  • 5.7.0                                ...           9 months ago
  • 5.6.2                                ...           9 months ago
  • 5.6.1                                ...           9 months ago
  • 5.6.0                                ...           10 months ago
  • 5.5.2                                ...           10 months ago
  • 5.5.1                                ...           10 months ago
  • 5.5.0                                ...           10 months ago
  • 5.4.1                                ...           a year ago
  • 5.4.0                                ...           a year ago
  • 5.3.1                                ...           a year ago
  • 5.3.0                                ...           a year ago
  • 5.2.1                                ...           a year ago
  • 5.2.0                                ...           a year ago
  • 5.1.0                                ...           a year ago
  • 5.0.2                                ...           a year ago
  • 5.0.1                                ...           a year ago
  • 5.0.0                                ...           a year ago
  • 5.0.0-beta.54                                ...           a year ago
  • 5.0.0-beta.53                                ...           a year ago
  • 5.0.0-beta.52                                ...           a year ago
  • 5.0.0-beta.51                                ...           a year ago
  • 5.0.0-beta.50                                ...           a year ago
  • 5.0.0-beta.49                                ...           a year ago
  • 5.0.0-beta.48                                ...           a year ago
  • 5.0.0-beta.47                                ...           a year ago
  • 5.0.0-beta.46                                ...           a year ago
  • 5.0.0-beta.45                                ...           a year ago
  • 5.0.0-beta.44                                ...           a year ago
  • 5.0.0-beta.43                                ...           a year ago
  • 5.0.0-beta.42                                ...           a year ago
  • 5.0.0-beta.41                                ...           a year ago
  • 5.0.0-beta.40                                ...           a year ago
  • 5.0.0-beta.39                                ...           a year ago
  • 5.0.0-beta.38                                ...           a year ago
  • 5.0.0-beta.37                                ...           a year ago
  • 4.38.3                                ...           a year ago
  • 5.0.0-beta.36                                ...           a year ago
  • 5.0.0-beta.35                                ...           a year ago
  • 5.0.0-beta.34                                ...           a year ago
  • 5.0.0-beta.33                                ...           a year ago
  • 5.0.0-beta.32                                ...           a year ago
  • 5.0.0-beta.31                                ...           a year ago
  • 5.0.0-beta.30                                ...           a year ago
  • 5.0.0-beta.29                                ...           a year ago
  • 5.0.0-beta.28                                ...           a year ago
  • 5.0.0-beta.27                                ...           a year ago
  • 5.0.0-beta.26                                ...           a year ago
  • 5.0.0-beta.25                                ...           a year ago
  • 5.0.0-beta.24                                ...           a year ago
  • 5.0.0-beta.23                                ...           a year ago
  • 5.0.0-beta.22                                ...           a year ago
  • 5.0.0-beta.21                                ...           a year ago
  • 5.0.0-beta.20                                ...           a year ago
  • 4.38.2                                ...           a year ago
  • 5.0.0-beta.19                                ...           a year ago
  • 4.38.1                                ...           a year ago
  • 5.0.0-beta.18                                ...           a year ago
  • 5.0.0-beta.17                                ...           a year ago
  • 5.0.0-beta.16                                ...           a year ago
  • 5.0.0-beta.15                                ...           a year ago
  • 4.38.0                                ...           a year ago
  • 5.0.0-beta.14                                ...           a year ago
  • 5.0.0-beta.13                                ...           a year ago
  • 5.0.0-beta.12                                ...           a year ago
  • 5.0.0-beta.11                                ...           a year ago
  • 5.0.0-beta.10                                ...           2 years ago
  • 5.0.0-beta.9                                ...           2 years ago
  • 4.37.2                                ...           2 years ago
  • 5.0.0-beta.8                                ...           2 years ago
  • 5.0.0-beta.7                                ...           2 years ago
  • 5.0.0-alpha.6                                ...           2 years ago
  • 5.0.0-alpha.5                                ...           2 years ago
  • 5.0.0-alpha.4                                ...           2 years ago
  • 5.0.0-alpha.3                                ...           2 years ago
  • 5.0.0-alpha.2                                ...           2 years ago
  • 4.37.1                                ...           2 years ago
  • 4.37.0                                ...           2 years ago
  • 4.36.0                                ...           2 years ago
  • 4.35.4                                ...           2 years ago
  • 4.35.3                                ...           2 years ago
  • 5.0.0-alpha.1                                ...           2 years ago
  • 4.35.2                                ...           2 years ago
  • 4.35.1                                ...           2 years ago
  • 4.35.0                                ...           2 years ago
  • 4.34.0                                ...           2 years ago
  • 4.33.6                                ...           2 years ago
  • 4.33.5                                ...           2 years ago
  • 4.33.4                                ...           2 years ago
  • 4.33.3                                ...           2 years ago
  • 4.33.2                                ...           2 years ago
  • 4.33.1                                ...           2 years ago
  • 4.33.0                                ...           2 years ago
  • 5.0.0-alpha.0                                ...           2 years ago
  • 4.32.1                                ...           3 years ago
  • 4.32.0                                ...           3 years ago
  • 4.31.0                                ...           3 years ago
  • 4.30.6                                ...           3 years ago
  • 4.30.5                                ...           3 years ago
  • 4.30.4                                ...           3 years ago
  • 4.30.3                                ...           3 years ago
  • 4.30.2                                ...           3 years ago
  • 4.30.1                                ...           3 years ago
  • 4.30.0                                ...           3 years ago
  • 4.29.0                                ...           3 years ago
  • 4.28.1                                ...           3 years ago
  • 4.28.0                                ...           3 years ago
  • 4.27.0                                ...           3 years ago
  • 4.26.1                                ...           3 years ago
  • 4.26.0                                ...           3 years ago
  • 4.27.0-pr2674-2021926101023                                ...           3 years ago
  • 4.26.0-pr2674-202191175724                                ...           3 years ago
  • 4.26.0-pr2674-20219612435                                ...           3 years ago
  • 4.26.0-pr2674-20219515814                                ...           3 years ago
  • 4.25.0                                ...           3 years ago
  • 4.24.1                                ...           3 years ago
  • 4.24.0                                ...           3 years ago
  • 4.23.4                                ...           3 years ago
  • 4.23.3                                ...           3 years ago
  • 4.23.2                                ...           3 years ago
  • 4.23.1                                ...           3 years ago
  • 4.23.0                                ...           3 years ago
  • 4.22.0                                ...           3 years ago
  • 4.20.2                                ...           3 years ago
  • 4.20.1                                ...           3 years ago
  • 4.20.0                                ...           3 years ago
  • 4.19.2                                ...           3 years ago
  • 4.19.1                                ...           4 years ago
  • 4.19.0                                ...           4 years ago
  • 4.18.0                                ...           4 years ago
  • 4.17.1                                ...           4 years ago
  • 4.17.0                                ...           4 years ago
  • 4.16.2                                ...           4 years ago
  • 4.16.1                                ...           4 years ago
  • 4.16.0                                ...           4 years ago
  • 4.15.4                                ...           4 years ago
  • 4.15.3                                ...           4 years ago
  • 4.15.2                                ...           4 years ago
  • 4.15.1                                ...           4 years ago
  • 4.15.0                                ...           4 years ago
  • 4.14.1                                ...           4 years ago
  • 4.14.0                                ...           4 years ago
  • 4.13.0                                ...           4 years ago
  • 4.12.0                                ...           4 years ago
  • 4.11.0                                ...           4 years ago
  • 4.10.0                                ...           4 years ago
  • 4.9.1                                ...           5 years ago
  • 4.9.0                                ...           5 years ago
  • 4.8.0                                ...           5 years ago
  • 4.7.8                                ...           5 years ago
  • 4.7.7                                ...           5 years ago
  • 4.7.6                                ...           5 years ago
  • 4.7.5                                ...           5 years ago
  • 4.7.3                                ...           5 years ago
  • 4.7.2                                ...           5 years ago
  • 4.7.1                                ...           5 years ago
  • 4.7.0                                ...           5 years ago
  • 4.7.0-rc7                                ...           5 years ago
  • 4.7.0-rc6                                ...           5 years ago
  • 4.7.0-rc5                                ...           5 years ago
  • 4.7.0-rc4                                ...           5 years ago
  • 4.7.0-rc3                                ...           5 years ago
  • 4.7.0-rc2                                ...           5 years ago
  • 4.7.0-rc1                                ...           5 years ago
  • 4.6.7                                ...           5 years ago
  • 4.6.6                                ...           5 years ago
  • 4.6.5                                ...           5 years ago
  • 4.6.4                                ...           5 years ago
  • 4.6.3                                ...           5 years ago
  • 4.6.2                                ...           5 years ago
  • 4.6.1                                ...           5 years ago
  • 4.6.0                                ...           5 years ago
  • 4.6.0-rc4                                ...           5 years ago
  • 4.6.0-rc3                                ...           5 years ago
  • 4.6.0-rc2                                ...           5 years ago
  • 4.6.0-rc1                                ...           5 years ago
  • 4.5.0                                ...           6 years ago
  • 4.4.0                                ...           6 years ago
  • 4.3.3                                ...           6 years ago
  • 4.3.2                                ...           6 years ago
  • 4.3.1                                ...           6 years ago
  • 4.3.0                                ...           6 years ago
  • 4.2.4                                ...           6 years ago
  • 4.2.3                                ...           6 years ago
  • 4.2.2                                ...           6 years ago
  • 4.2.1                                ...           6 years ago
  • 4.2.0                                ...           6 years ago
  • 4.1.2                                ...           6 years ago
  • 4.1.1                                ...           6 years ago
  • 4.1.0                                ...           6 years ago
  • 4.0.1                                ...           6 years ago
  • 4.0.0                                ...           6 years ago
  • 4.0.0-15                                ...           6 years ago
  • 4.0.0-14                                ...           6 years ago
  • 4.0.0-13                                ...           6 years ago
  • 4.0.0-12                                ...           6 years ago
  • 4.0.0-11                                ...           6 years ago
  • 4.0.0-10                                ...           6 years ago
  • 4.0.0-9                                ...           6 years ago
  • 4.0.0-8                                ...           6 years ago
  • 4.0.0-7                                ...           6 years ago
  • 4.0.0-6                                ...           6 years ago
  • 4.0.0-1                                ...           6 years ago
  • 4.0.0-0                                ...           6 years ago
  • 3.3.3                                ...           6 years ago
  • 3.3.2                                ...           6 years ago
  • 3.3.1                                ...           6 years ago
  • 3.3.0                                ...           6 years ago
  • 3.2.1                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.4                                ...           7 years ago
  • 3.0.3                                ...           7 years ago
  • 3.0.2                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.2.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Total 3
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (21)

© 2010 - cnpmjs.org x YWFE | Home | YWFE