# fsmx

> Finite State Machine

Latest version **1.0.2** (published 2020-05-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install fsmx
pnpm add fsmx
yarn add fsmx
bun add fsmx
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-05-11 |
| First published | 2018-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tn |
| Maintainers | nurislamov |
| Keywords | FSM, finite, state, machine, mechanism, algorithm |

## Links

- npm: https://www.npmjs.com/package/fsmx
- Homepage: https://github.com/tn/fsmx
- npm.io page: https://npm.io/package/fsmx

## 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-05-11
- 1.0.1 — 2020-05-11
- 1.0.0 — 2020-05-11
- 0.1.2 — 2018-07-16
- 0.0.2 — 2018-07-05
- 0.0.1 — 2018-06-07

## README

# FSMX [![Build Status](https://travis-ci.org/tn/fsmx.svg?branch=master)](https://travis-ci.org/tn/fsmx)
Simple Finite State Machine based on async/await events and actions

## Documentation
### Setup
```
import fsmx from 'fsmx'

const fsm = new fsmx({
    initial: 'initial',
    actions: {
        waitForCmd: {from: ['initial', 'end', 'error'], to: 'wait'},
        listenForCmd: {from: 'wait', to: 'listen'},
        collectData: {from: 'listen', to: 'calculate'},
        giveAnswer: {from: 'calculate', to: 'speak'},
        endCmd: {from: 'speak', to: 'end'},
        wrongWay: {from: 'calculate', to: 'error'}
    },
    events: {
        async onLeaveState (from, to) {
            // do something asynchronous
        },
        async onEnterState (from, to) {
            // do something asynchronous
        }
    }
})
```

### Initial parameters
| Name           | Type          | Default   | Required |
|----------------|---------------|-----------|----------|
| initial        | String        | undefined | yes      |
| actions        | Object        | undefined | yes      |
| actions[].from | Array, String | -         | yes      |
| actions[].to   | String        | -         | yes      |
| events         | Object        | {}        | no       |

### Methods
`fsm.is('initial')` - check the current state

`fsm.can('wait')` - check possibility to transition to some state from the current state

`fsm.reset()` - reset the current state to initial
```
// Call actions to transition between states
(async () => {
    await fsm.waitForCmd()
    await fsm.listenForCmd()
    // ...
})()
```

## Scripts
`yarn start` - development

`yarn test` - tests

`yarn test:watch` - tests with watch flag

`yarn build` - build

`yarn coverage` - get coverage report

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