# st8

> Tiny state machine

Latest version **3.0.1** (published 2022-02-09) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2022-02-09 |
| First published | 2014-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Dmitry Ivanov |
| Maintainers | dfcreative, dy |
| Keywords | state, fsm, state machine, component, stateful |

## Links

- npm: https://www.npmjs.com/package/st8
- Repository: https://github.com/dy/st8
- Homepage: https://github.com/dy/st8#readme
- Issues: https://github.com/dy/st8/issues
- npm.io page: https://npm.io/package/st8

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

- 3.0.1 (latest) — 2022-02-09
- 2.1.0 (two) — 2022-02-16
- 3.0.0 — 2022-02-09
- 2.0.0 — 2019-01-24
- 1.1.1 — 2016-01-09
- 1.1.0 — 2015-04-28
- 1.0.5 — 2015-04-11
- 1.0.4 — 2015-01-04
- 1.0.3 — 2015-01-03
- 1.0.2 — 2015-01-02
- 1.0.1 — 2015-01-02
- 1.0.0 — 2015-01-01
- 0.1.27 — 2014-12-04
- 0.1.26 — 2014-12-04
- 0.1.25 — 2014-12-04
- … 29 more at https://npm.io/package/st8/versions

## README

# st8 [![test](https://github.com/dy/st8/actions/workflows/test.yml/badge.svg)](https://github.com/dy/st8/actions/workflows/test.yml) [![npm](https://img.shields.io/npm/v/st8)](http://npmjs.org/st8)

> Tiny state machine for structural describing behavior of components.


# Usage

```
$ npm install st8
```

## Standalone state machine

```js
import State from 'st8';

var state = new State({
	// enter, exit
	b: () => () => {},

	// enter shortcut, forwards to state d
	c: () => 'd',

	// shorter cut, redirects to state a
	d: 'a',

	// any other state
	_: 'a'
});


//enter state 'a', invoke corresponding callbacks
state.set('a');

//get current state
state.get(); // 'a'
```

## Define stateful object property

```js
import State from 'st8'

var state = new State({
	a() {
		// onenter
		this === target //true
	},

	b() {
		// onenter
		this === target //true
		return () => {
			// onexit
		}
	}

}, target);

Object.defineProperty(target, property, {
	set: function (value) {
		return state.set(value);
	},
	get: function () {
		return state.get();
	}
});
```

# API

### let state = new State(states [, context])

Create a new state machine based on the `states` object. Optionally pass a `context` for callbacks.

### state.get()

Get current state.

### state.set(value)

Transition to a new state, invoking necessary callbacks.

<!--
### state.subscribe((newState,oldState) => {})

Subscribe to state changes.

### state\[Symbol.observable\]()

Returns _Subscribable_ for interop with _Observables_ etc.
-->


<p align="center">🕉<p>

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