# fsm-base

> Finite state machine base class

Latest version **0.9.0** (published 2024-07-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install fsm-base
pnpm add fsm-base
yarn add fsm-base
bun add fsm-base
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2024-07-06 |
| First published | 2015-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=12.17 |
| Dependencies | 1 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Lloyd Brookes |
| Maintainers | 75lb |
| Keywords | finite, state, machine, base, class, isomorphic, load-anywhere |

## Links

- npm: https://www.npmjs.com/package/fsm-base
- Repository: https://github.com/75lb/fsm-base
- Homepage: https://github.com/75lb/fsm-base#readme
- Issues: https://github.com/75lb/fsm-base/issues
- npm.io page: https://npm.io/package/fsm-base

## Dependencies (1)

- [array-back](https://npm.io/package/array-back.md) ^6.2.2

## 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

- 0.9.0 (latest) — 2024-07-06
- 0.8.0 — 2023-03-11
- 0.7.0 — 2021-07-04
- 0.6.0 — 2020-05-30
- 0.5.2 — 2019-11-17
- 0.5.1 — 2019-11-15
- 0.5.0 — 2019-11-04
- 0.4.4 — 2019-03-03
- 0.4.3 — 2018-12-30
- 0.4.2 — 2018-12-28
- 0.4.1 — 2018-12-25
- 0.4.0 — 2018-12-24
- 0.3.0 — 2018-12-22
- 0.2.1 — 2018-02-16
- 0.2.0 — 2017-02-15
- … 1 more at https://npm.io/package/fsm-base/versions

## README

[![view on npm](https://badgen.net/npm/v/fsm-base)](https://www.npmjs.org/package/fsm-base)
[![npm module downloads](https://badgen.net/npm/dt/fsm-base)](https://www.npmjs.org/package/fsm-base)
[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/fsm-base)](https://github.com/75lb/fsm-base/network/dependents?dependent_type=REPOSITORY)
[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/fsm-base)](https://github.com/75lb/fsm-base/network/dependents?dependent_type=PACKAGE)
[![Node.js CI](https://github.com/75lb/fsm-base/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/fsm-base/actions/workflows/node.js.yml)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)

# fsm-base

Finite state machine.

Either mix it into an existing object:

```js
import StateMachine from 'fsm-base'

const device = {}
StateMachine.mixInto(device)

device._initStateMachine('offline', [
  { from: 'offline', to: 'connecting' },
  { from: 'connecting', to: 'online' },
  { from: ['connecting', 'online'], to: 'offline' }
])

device.onStateChange = function (state, prevState) {
  console.log(state, prevState)
}

device.state = 'connecting'
device.state = 'connecting' // should not trigger events again
device.state = 'online'
device.state = 'offline' // valid state move
```

..or define a class which extends it:

```js
class Device extends StateMachine {}

const device = new Device()
device._initStateMachine('offline', [
  { from: 'offline', to: 'connecting' },
  { from: 'connecting', to: 'online' },
  { from: ['connecting', 'online'], to: 'offline' }
])

device.onStateChange = function (state, prevState) {
  console.log(state, prevState)
}

device.state = 'connecting'
// etc
```

..or mix it into an existing class that does not `extend` fsm-base.

```js
class Device {}
StateMachine.mixInto(Device)

const device = new Device()
device._initStateMachine('offline', [
  { from: 'offline', to: 'connecting' },
  { from: 'connecting', to: 'online' },
  { from: ['connecting', 'online'], to: 'offline' }
])

device.onStateChange = function (state, prevState) {
  console.log(state, prevState)
}

device.state = 'connecting'
//etc
```

See the [API Documentation](https://github.com/75lb/fsm-base/tree/master/docs/api.md) for more information.

* * *

&copy; 2015-24 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

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