github

davidkpiano / xstate

  • воскресенье, 27 августа 2017 г. в 03:14:14
https://github.com/davidkpiano/xstate


Stateless JS Finite State Machines and Statecharts



XState

Simple, stateless JavaScript finite state machines and statecharts.

Why?

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

Visualizing state machines and statecharts

The JSON-based notation used here to declaratively represent finite state machines and statecharts can be copy-pasted here: https://codepen.io/davidkpiano/pen/ayWKJO/ which will generate interactive state transition diagrams.

Getting Started

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

Finite State Machines

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')
  .toString(); // toString() only works for non-parallel machines

// => 'yellow'

More examples coming soon!