# ts-machine

> A simple state machine with _good enough_ build time typechecking support.

Latest version **0.0.5** (published 2023-03-21) · 0 weekly downloads

## Install

```sh
npm install ts-machine
pnpm add ts-machine
yarn add ts-machine
bun add ts-machine
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2023-03-21 |
| First published | 2023-03-18 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | jacob-ebey |

## Links

- npm: https://www.npmjs.com/package/ts-machine
- Repository: https://github.com/jacob-ebey/ts-machine
- Homepage: https://github.com/jacob-ebey/ts-machine#readme
- Issues: https://github.com/jacob-ebey/ts-machine/issues
- npm.io page: https://npm.io/package/ts-machine

## Recent versions

- 0.0.5 (latest) — 2023-03-21
- 0.0.4 — 2023-03-18
- 0.0.3 — 2023-03-18
- 0.0.2 — 2023-03-18
- 0.0.1 — 2023-03-18

## README

# ts-machine ![min size](https://badgen.net/bundlephobia/min/ts-machine) ![minzip size](https://badgen.net/bundlephobia/minzip/ts-machine)

A simple state machine with _good enough_ build time typechecking support.

```sh
npm i ts-machine
```

## Example

Here is a simple stoplight example:

```ts
const lightMachine = defineMachine<null>()
  .states(["green", "yellow", "red"])
  .transitions({
    green: ["yellow"],
    yellow: ["red"],
    red: ["green"],
  })
  .events(["SWITCH"])
  .on("SWITCH", ["green"], () => "yellow")
  .on("SWITCH", ["yellow"], ({}) => "red")
  .on("SWITCH", ["red"], () => "green");

const light = lightMachine.create("red", null);

assert.equal(light.state, "red");
light.emit("SWITCH");
await waitForState(light, "green");
light.emit("SWITCH");
await waitForState(light, "yellow");
light.emit("SWITCH");
await waitForState(light, "red");
```

More examples can be found in the tests directory:

- fetcher - [tests/fetcher.test.ts](./tests/fetcher.test.ts)
- router - [tests/router.test.ts](./tests/router.test.ts)
- stoplight - [tests/stoplight.test.ts](./tests/stoplight.test.ts)

## Helpers

### waitForState

```ts
import { type Machine } from "ts-machine";

export function waitForState<M extends Machine<any, any>>(
  machine: M,
  state: M["state"] | M["state"][],
  next: boolean = true
) {
  return new Promise<void>((resolve) => {
    if (machine.state === state) {
      resolve();
      return;
    }
    const unsubscribe = machine.subscribe((machine) => {
      if (machine.state === state) {
        unsubscribe();
        resolve();
        return;
      }

      if (next) {
        unsubscribe();
        throw new Error(
          `Expected state ${
            typeof state === "string" ? state : (state as string[]).join(",")
          } but got ${machine.state}`
        );
      }
    });
  });
}
```

---
_Source: https://npm.io/package/ts-machine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
