@alcadica/state-manager-transactions
v1.0.0
Published
Transactions for state-manager
Downloads
2
Readme
@alcadica/state-manager-transactions
This plugin enables transactional state management
Install
npm i --save @alcadica/{state-manager,state-manager-transactions}
Usage
import createStore from '@alcadica/state-manager';
import Transactions from '@alcadica/state-manager-transactions';
const store = createStore({
test: 0,
});
store.use(Transactions());
const increment = createStore.createAction('increment');
store.reducer.connect((state, action, update) => {
update({
test: state.test + (typeof action.payload === 'number' ? action.payload : 1),
});
});
store.dispatch(Transactions.begin());
store.dispatch(increment()); // state.test is '1'
store.dispatch(increment()); // state.test is '2'
store.dispatch(increment()); // state.test is '3'
store.dispatch(increment(7)); // state.test is '10'
store.dispatch(Transactions.undo());
console.log(store.getState().test) // outputs 0, we cancelled the transaction
store.dispatch(Transactions.begin());
store.dispatch(increment()); // state.test is '1'
store.dispatch(increment()); // state.test is '2'
store.dispatch(increment()); // state.test is '3'
store.dispatch(increment(7)); // state.test is '10'
store.dispatch(Transactions.end());
console.log(store.getState().test) // outputs 10, we ended the transaction