2.1.1 • Published 4 years ago

rx-machine v2.1.1

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

RxMachine

Water meets metal

RxMachine was created to get the stable setup of a state machine coupled with the reactiveness from RxJS. We also get the benefits of TypeScript to make sure that nothing falls outside the state chart.

Example usage

import { createRxm } from "./index";
import { skip, take } from "rxjs/operators";
import { createAction } from "./create-action";

test("createRxm", async () => {
  type Chart = {
    started: ["end"];
    ended: ["restart"];
  };

  type StartedStore = {
    state: "started";
    ctx: number;
  };

  type EndedStore = {
    state: "ended";
    ctx: number;
  };

  type Store = StartedStore | EndedStore;

  const chart: Chart = {
    started: ["end"],
    ended: ["restart"]
  };

  const actions = {
    end: createAction(
      (s: Store, ctx: number): Store => ({
        state: "ended",
        ctx
      })
    ),
    restart: createAction(
      (s: Store, ctx: number): Store => ({
        state: "started",
        ctx
      })
    )
  };

  const initialStore: Store = {
    state: "started",
    ctx: 123
  } as Store;

  const { store } = createRxm(chart, initialStore, actions);

  const result = store
    .pipe(
      skip(1),
      take(1)
    )
    .toPromise();

  actions.end.trigger(1);

  expect(await result).toEqual({ state: "ended", ctx: 1 });
});
2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago