All Projects → chungchiehlun → redux-statechart

chungchiehlun / redux-statechart

Licence: other
A tiny library make redux state possessed with both finite and infinite states.

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to redux-statechart

simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-3.85%)
Mutual labels:  statechart, finite-state-machine
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+81769.23%)
Mutual labels:  statechart, finite-state-machine
StateBuilder
State machine code generator for C++ and Java.
Stars: ✭ 30 (+15.38%)
Mutual labels:  statechart, finite-state-machine
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (+650%)
Mutual labels:  finite-state-machine
ember-statechart-component
Statecharts as components. No classes. Pure declarative state transitions.
Stars: ✭ 28 (+7.69%)
Mutual labels:  statechart
statechart
A rust implementation of statecharts: hierarchical, reactive state machines
Stars: ✭ 41 (+57.69%)
Mutual labels:  statechart
regexjs
A fast and minimal regular expression engine.
Stars: ✭ 64 (+146.15%)
Mutual labels:  finite-state-machine
xstate-addon
Storybook addon for xstate
Stars: ✭ 20 (-23.08%)
Mutual labels:  statechart
Python Statemachine
Python Finite State Machines made easy.
Stars: ✭ 184 (+607.69%)
Mutual labels:  finite-state-machine
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+70284.62%)
Mutual labels:  statechart
qp-arduino
QP real-time embedded frameworks/RTOS for Arduino (AVR and SAM)
Stars: ✭ 37 (+42.31%)
Mutual labels:  statechart
use-secret-code
Custom hook for adding cheat codes to your React app.
Stars: ✭ 16 (-38.46%)
Mutual labels:  statechart
qm
QM model-based design tool and code generator based on UML state machines
Stars: ✭ 54 (+107.69%)
Mutual labels:  statechart
Pluck
Pluck text in a fast and intuitive way 🐓
Stars: ✭ 202 (+676.92%)
Mutual labels:  finite-state-machine
datafsm
Machine Learning Finite State Machine Models from Data with Genetic Algorithms
Stars: ✭ 14 (-46.15%)
Mutual labels:  finite-state-machine
Awesome Fsm
🤖 A curated list of awesome resources related to finite state machines and statecharts.
Stars: ✭ 189 (+626.92%)
Mutual labels:  finite-state-machine
gamma
An Eclipse-based modeling framework for the component-based design and analysis of reactive systems
Stars: ✭ 21 (-19.23%)
Mutual labels:  statechart
flviz
FLVIz - Finite Automata Simulator written in QT/Graphviz
Stars: ✭ 36 (+38.46%)
Mutual labels:  finite-state-machine
ssfst
📜 Rewrite text in linear time.
Stars: ✭ 78 (+200%)
Mutual labels:  finite-state-machine
ScxmlEditor-Tutorial
ScxmlEditor - powerful tool for creating, editing and debugging scxml files
Stars: ✭ 33 (+26.92%)
Mutual labels:  statechart

redux-statechart

redux + xstate = awesome !

Redux is a predictable state container for JavaScript apps. One of the redux principles is that your app state is stored in a single tree. Accordingly, UI variables (isLoading, isVisible) and side effects (data fetching response) are all states. When the operation of your app get complicated (it's usually happens quickly), the state always become unpredictable.

Xstate is functional, stateless JavaScript finite state machines (FSM) and statecharts. An FSM is defined by a list of its states, its initial state, and the conditions for each transition. A statechart is a state machine where each state in the state machine may define its own subordinate state machines, called substates. Those states can again define substates. These are useful for declaratively describing the behavior of your app, from the individual components to the overall app logic.

Deterministic state not only reduce time fixing unpredictable and exploded state but also provide a common language for designers & developers. On the other hand, uncertain or extended state are regular when handling side effect in the real world app. Generally speaking, both infinite and finite states are necessary.

Redux do not strictly limit state shape but xstate have to define a machine configuration. Redux-statechart is a tiny library make redux state possessed with both finite and infinite states in the underlying redux pattern.

Usage Example

First, create a statechart machine. Please note that xstate must be version 4.x.

const starWarsMachine = Machine({
  id: "starWars",
  initial: "idle",
  states: {
    idle: {
      on: {
        REQUEST: {
          target: "pending",
          actions: ["alertStartingFirstRequest"]
        }
      },
      onExit: "alertMayTheForceBeWithYou"
    },
    pending: {
      on: {
        SUCCESS: "fulfilled",
        FAILURE: "rejected"
      },
      onEntry: "fetchPerson",
      onExit: "alertRequestFinished"
    }
  }
});

Redux-statechart take array of statechart machiness as the only argument and return a higher order reducer (HOR) and an action creator. HOR takes a regular reducer as the first argument and an optional initial state as the second. Note the initial state must be object.

import RS from "redux-statechart";

// reducerEnhancer is a Higher Order Reducer.
const { machineActionCreator, reducerEnhancer } = RS([starWarsMachine]);

const extReducer = (state, action) => {
  switch (action.type) {
    case "INCREMENT":
      return { ...state, counter: state.counter + 1 };
    case "DECREMENT":
      return { ...state, counter: state.counter - 1 };
    default:
      return state;
  }
};
const enhancedReducer = reducerEnhancer(extReducer, { counter: 0 });

const store = createStore(enhancedReducer);

The principle of reducer do not restrict the shape of state. A reducer is hard to distinguish between finite and infinite state.

In the convention of redux-statechart, the finite state are appended with Mach.

Finally, whether finite or infinite state, call dispatch to update the state. For convenience, redux-statechart contains another returned value, machineActionCreator. It takes machine id and machine event as required arguments and return an action you could dispatch it.

store.dispatch(machineActionCreator("starWars", "REQUEST"));
// => State{ counter: 0, starWarsMach: { value: "pending", ... }}

store.dispatch({ type: "INCREMENT" });
// => State{ counter: 1, starWarsMach: { value: "pending", ... }}

Issues

Feel free to submit issues and enhancement requests.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].