# sweet-state

> finite state machine written in TypeScript

Latest version **1.0.2** (published 2020-01-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install sweet-state
pnpm add sweet-state
yarn add sweet-state
bun add sweet-state
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-01-24 |
| First published | 2020-01-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 624.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Colin Knebl |
| Maintainers | colinknebl |
| Keywords | state, machine, typescript |

## Links

- npm: https://www.npmjs.com/package/sweet-state
- Repository: https://github.com/colinknebl/sweet-state
- Homepage: https://github.com/colinknebl/sweet-state#readme
- Issues: https://github.com/colinknebl/sweet-state/issues
- npm.io page: https://npm.io/package/sweet-state

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-01-24
- 1.0.1 — 2020-01-06
- 1.0.0 — 2020-01-06

## README

# sweet-state
A finite state machine written in TypeScript

## Example Usage

### Basic Machine Setup (file name: <i>machine.ts</i>)

```TypeScript
// 1. import the Machine constructor and StateType enum
import { Machine, StateType } from 'sweet-state';

// 2. set the machine states in an enum
export enum MachineStates {
  idle = "idle",
  loading = "loading",
  error = "error",
  success = "success"
}

// 3. create events
export enum MachineEvents {
    submit = 'SUBMIT'
}

// action callbacks must return type of: Promise<void>
function loadingCallback(_: Machine<MachineStates>): Promise<void> {
    const randomNumber = Math.floor(Math.random() * 2);
    if (randomNumber === 0) {
        return Promise.resolve();
    } else {
        const error = new Error("THERE WAS A TERRIBLE ERROR");
        // return Promise.reject(error);
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                reject(error);
            }, 2000);
        });
    }
}

// 4. set up the states
const states = {
    [MachineStates.idle]: {
        on: {
            [MachineEvents.submit]: MachineStates.loading
        }
    },
    [MachineStates.loading]: {
        action: {
            callback: loadingCallback,
            onError: MachineStates.error,
            onSuccess: MachineStates.success
        }
    },
    [MachineStates.error]: {
        on: {
            [MachineEvents.submit]: MachineStates.loading
        }
    },
    [MachineStates.success]: {
        type: StateType.final
    }
}

// 5. set the initial state
const initial = MachineStates.idle;

// 6. initialize the machine
const machine = new Machine<MachineStates>({
    initial,
    states
});

// 7. start the machine
machine.start();
```

### Listening to state changes
```TypeScript
function listenCallback(currentState, error) {
    // ...do something
}
machine.listen(listenCallback);
```

### Sending events
```TypeScript
machine.send(MachineEvents.submit)
```

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