fsm-redux

1.0.0 • Public • Published

fsm-redux

NPM version Build Status Coverage Status

Lightweight library for Finite State Machine support in Redux applications. This library implements FSM pattern as swappable redux reducers. In a configuration object you specify which reducer should be used for each state of FSM and actions which trigger state change.

Example

var params = {
    statusKey: 'status', // name of field that will hold current state (optional parameter)
    initial: 'INIT', // name of initial state
    states: {
      'INIT': {
        reducer: function(state, action) {}, // reducer used for state 'INIT'
        accepts: {
          'LOAD_ACTION': 'LOADING' // action with type 'LOAD_ACTION' will switch FSM to 'LOADING' state
        }
      },
      'LOADING': {
        reducer: function(state, action) {}, // reducer used for state 'LOADING'
        accepts: {
          'LOAD_DONE_ACTION': 'LOADING_SUCCESS', // action 'LOAD_DONE_ACTION' will switch FSM to 'LOADING_SUCCESS' state
          'LOAD_FAIL_ACTION': 'LOADING_FAILURE'  // action 'LOAD_FAIL_ACTION' will switch FSM to 'LOADING_FAILURE' state
        }
      },
      'LOADING_SUCCESS': {
        reducer: function(state, action) {}, // reducer used for state 'LOADING_SUCCESS'
        accepts: {
          'LOAD_ACTION': 'LOADING' // action 'LOAD_ACTION' will switch FSM to 'LOADING' state
        }
      },
      'LOADING_FAILURE': {
        reducer: function(state, action) {}, // reducer used for state 'LOADING_FAILURE'
        accepts: {
          'LOAD_ACTION': 'LOADING' // action 'LOAD_ACTION' will switch FSM to 'LOADING' state
        }
      }
    }
  };
  
var fsm = createFSM(params, initialState); // create FSM reducer

License

MIT

Package Sidebar

Install

npm i fsm-redux

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • quadreex