Nightly Perpetrated Mischief
Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@alcadica/state-manager-transactions

1.0.0 • Public • Published

@alcadica/state-manager-transactions

Coverage Maintainability

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

Licence

MIT

Install

npm i @alcadica/state-manager-transactions

DownloadsWeekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

8.14 kB

Total Files

7

Last publish

Collaborators

  • avatar
  • avatar