The transaction extension adds the ability to rollback a set of mutations in the event of an error.
Follow the steps below to get started using the transaction extension.
Before installing this extension make sure you have installed harlem
.
yarn add @harlem/extension-transaction
# or
npm install @harlem/extension-transaction
To get started simply register this extension with the store you wish to extend.
import transactionExtension from '@harlem/extension-transaction';
import {
createStore
} from 'harlem';
const STATE = {
firstName: 'Jane',
lastName: 'Smith'
};
const {
state,
getter,
mutation,
transaction,
onBeforeTransaction,
onAfterTransaction,
onTransactionSuccess,
onTransactionError,
} = createStore('example', STATE, {
extensions: [
transactionExtension()
]
});
The transaction extension adds several new methods to the store instance (highlighted above).
A transaction can be defined the same way you define any other core functionality (eg. getter
, mutation
etc.).
export default transaction('my-transaction', (payload, mutate) => {
/*
Any mutations run during this method
will be rolled back if an error occurs
*/
});
- This extension comes with a performance cost. The entire state tree is cloned and stored before a transaction is run in order to rollback in the event of an error.
- This extension does not protect against issues attempting to roll back state in async scenarios