0.0.11 • Published 5 years ago

carry-on-transaction v0.0.11

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

carry-on-transaction

carry-on plugin to add commit/rollback support to the store.

Usage

import transaction from "carry-on-transaction";
import { Store, State } from "carry-on-react";

let commits = 0;
let rollbacks = 0;
let commitException = 0;
let rollbackException = 0;

const store = ({ dispatch, query, state }) => {
  const inc = () =>
    dispatch(
      state => ({
        ...state,
        counter: state.counter + 1
      }),
      "Increment"
    );

  const dec = () =>
    dispatch(
      state => ({
        ...state,
        counter: state.counter - 1
      }),
      "Decrement"
    );

  function beginClick(msg) {
    // start transaction
    state.begin();
  }

  function commitClick() {
    const before = query();
    try {
      const after = state.commit();
    } catch (e) {
      commitException += 1;
    }
    commits++;
  }

  function rollbackClick() {
    const before = query();
    try {
      const after = state.rollback();
    } catch (e) {
      rollbackException += 1;
    }
    rollbacks++;
  }

  return {
    counter: 0,
    inc,
    dec,
    beginClick,
    commitClick,
    rollbackClick
  };
};

const App = () => (
  <Store init={store} plugins={[transaction()]}>
    <State>
      {({ counter, inc, dec, beginClick, commitClick, rollbackClick }) => (
        <>
          <div>Counter: {counter}</div>
          <button onClick={inc}>+</button>
          <button onClick={dec}>-</button>
          <button onClick={beginClick}>begin tx</button>
          <button onClick={commitClick}>commit</button>
          <button onClick={rollbackClick}>rollback</button>
        </>
      )}
    </State>
  </Store>
);
0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago