0.0.45-alpha • Published 2 years ago

@crux/redux-machine v0.0.45-alpha

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

@crux/redux-machine

machine is an asynchronous Finite State Machine for redux. It's fully-typed, with action names and params inferred from your reducer definitions.

Installation

npm install --save @crux/redux-machine

Usage

interface LightSwitchData {
  state: string;
  meta: number;
}

const initialState: LightSwitchData = {
  meta: 0,
  state: 'off'
};

const lightSwitch = {
  off: {
    switchOn: (state: LightSwitchData, payload: number) => ({
      ...state,
      state: 'on',
      meta: payload,
    }),
  },
  on: {
    switchOff: (state: LightSwitchData) => ({
      ...state,
      state: 'off',
    }),
  },
};

const { actions, reducer } = machine('lightSwitch', lightSwitch, initialState);

// Add reducer as `lightSwitch`. Then just dispatch actions:

store.dispatch(actions.switchOn(2)); // switchOn is fully typed (number and type of arguments enforced by TS)

expect(store.getState().lightSwitch).toEqual({
  state: 'on',
  meta: 2
});

store.dispatch(actions.switchOn(1));

expect(store.getState().lightSwitch).toEqual({
  state: 'on',
  meta: 2 // Switch already 'on', so we didn't update here
});

store.dispatch(actions.switchOff(1));

expect(store.getState().lightSwitch).toEqual({
  state: 'off',
  meta: 1 // Switch already 'on', so we didn't update here
});
0.0.45-alpha

2 years ago

0.0.40-alpha

2 years ago

0.0.42-alpha

2 years ago

0.0.43-alpha

2 years ago

0.0.44-alpha

2 years ago

0.0.41-alpha

2 years ago

0.0.37-alpha

2 years ago

0.0.39-alpha

2 years ago

0.0.36-alpha

2 years ago

0.0.38-alpha

2 years ago

0.0.35-alpha

2 years ago