fsm-redux
v1.0.0
Published
Lightweight library for Finite State Machine support in Redux applications
Downloads
10
Readme
fsm-redux
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