@alcadica/state-manager-history

A history recorder for state-manager

Usage no npm install needed!

<script type="module">
  import alcadicaStateManagerHistory from 'https://cdn.skypack.dev/@alcadica/state-manager-history';
</script>

README

state-manager-history

branch status
master Build Status
dev Build Status

Install

npm i --save @alcadica/{state-manager,state-manager-history}

Usage

With this middleware you can rollback your store at a previous state.

Example:

import createStore from '@alcadica/state-manager';
import StoreHistory from '@alcadica/state-manager-history';

const store = createStore({ foo: 0 });
const config = { maxRecords: 25 };

store.use(StoreHistory(config));

const increment = store.createAction<number>('increment');

store.reducer.connect((state, action, update) => {
  if (action.type === increment.type) {
    update({ foo: state.foo + action.payload });
  }
});

store.dispatch(increment(1)); // state.foo is 1
store.dispatch(increment(1)); // state.foo is 2

store.dispatch(StoreHistory.rollbackAction()) // state.foo is 1
store.getState() // { foo: 1 }
store.dispatch(StoreHistory.rollbackAction()) // state.foo is 0
store.getState() // { foo: 0 }