@harlem/extension-transaction
TypeScript icon, indicating that this package has built-in type declarations

3.1.8 • Public • Published

Harlem Transaction Extension

The transaction extension adds the ability to rollback a set of mutations in the event of an error.

Getting Started

Follow the steps below to get started using the transaction extension.

Installation

Before installing this extension make sure you have installed harlem.

yarn add @harlem/extension-transaction
# or
npm install @harlem/extension-transaction

Registration

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).

Usage

Defining a transaction

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

/@harlem/extension-transaction/

    Package Sidebar

    Install

    npm i @harlem/extension-transaction

    Homepage

    harlemjs.com

    Weekly Downloads

    3

    Version

    3.1.8

    License

    MIT

    Unpacked Size

    18.2 kB

    Total Files

    12

    Last publish

    Collaborators

    • andrewcourtice