# asyncmachine

> Hybrid State Machine

Latest version **3.4.1** (published 2018-09-25) · MIT license · 0 weekly downloads

## Install

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

Provides the command `am-types`.

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.4.1 |
| Published | 2018-09-25 |
| First published | 2012-12-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 901.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 67 |
| Author | Tobiasz Cudnik |
| Maintainers | tobiasz.cudnik |
| Keywords | async, statemachine, eventemitter, states, transition, eventloop, declarative, relational, aop, fsm, actor-model, dependency-graph |

## Links

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

## Dependencies (2)

- [commander](https://npm.io/package/commander.md) ^2.14.1
- [simple-random-id](https://npm.io/package/simple-random-id.md) ^1.0.1

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 3.4.1 (latest) — 2018-09-25
- 3.4.0 — 2018-06-23
- 3.3.6 — 2018-06-11
- 3.3.5 — 2018-06-11
- 3.3.4 — 2018-06-11
- 3.3.3 — 2018-06-10
- 3.3.2 — 2018-06-10
- 3.3.1 — 2018-05-24
- 3.3.0 — 2018-05-22
- 3.2.8 — 2018-05-17
- 3.2.7 — 2018-05-11
- 3.2.6 — 2018-05-07
- 3.2.5 — 2018-05-07
- 3.2.4 — 2018-05-02
- 3.2.3 — 2018-03-01
- … 14 more at https://npm.io/package/asyncmachine/versions

## README

**AsyncMachine** is a relational state machine made for declarative flow control. It supports multiple states simultaneously, executes methods based on a dependency graph and provides an event emitter.

It allows for easy:

* state management
* parallel tasks
* loose coupling
* resource allocation / disposal
* exception handling
* fault tolerance
* method cancellation

It supports forming a network of state machines and can also be used as a simple state management library. Gzipped code is 7.5kb.

## Install

```
npm i asyncmachine
```

## Documentation

* [AsyncMachine - The Definitive Guide](https://github.com/TobiaszCudnik/asyncmachine/wiki/AsyncMachine-The-Definitive-Guide) (wiki)<br>
  [PDF version](https://github.com/TobiaszCudnik/asyncmachine/raw/gh-pages/AsyncMachine-The-Definitive-Guide.pdf) (25 pages, 1.5mb)
* [API docs](https://tobiaszcudnik.github.io/asyncmachine/api) (TypeScript)
  * [machine() factory](https://tobiaszcudnik.github.io/asyncmachine/api/index.html#machine)
  * [AsyncMachine class](https://tobiaszcudnik.github.io/asyncmachine/api/classes/asyncmachine.html)
  * [Transition class](https://tobiaszcudnik.github.io/asyncmachine/api/classes/transition.html)
  * [List of emitted events](https://tobiaszcudnik.github.io/asyncmachine/api/interfaces/iemit.html)
* [Roadmap](https://github.com/TobiaszCudnik/asyncmachine/blob/master/TODO.md)

Components:

* states
* transitions
* relations
* clocks
* pipes
* queues

Features:

* synchronous mutations
* negotiation
* cancellation
* automatic states
* exception handling
* [visual inspector / debugger](https://github.com/TobiaszCudnik/asyncmachine-inspector)

## Examples

### Dry Wet

This basic examples makes use of: `states`, `transitions`, `relations` and `synchronous mutations`.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1edd421eaec500126c11ce)
* [Inspect on StackBlitz](https://stackblitz.com/edit/asyncmachine-example-dry-wet?file=index.ts)

```typescript
import { machine } from 'asyncmachine'
// define
const state = {
  Wet: { require: ['Water'] },
  Dry: { drop: ['Wet'] },
  Water: { add: ['Wet'], drop: ['Dry'] }
}
// initialize
const example = machine(state)
// make changes
example.add('Dry')
example.add('Water')
// check the state
example.is() // -> [ 'Wet', 'Water' ]
```

[![example](https://raw.githubusercontent.com/TobiaszCudnik/asyncmachine/gh-pages/images/example.gif)](https://stackblitz.com/edit/asyncmachine-example-dry-wet?file=index.ts)

### Negotiation

Presents how the `state negotiation` works.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1ed850c6dc1f0012db1346)
* [Inspect on StackBlitz](https://stackblitz.com/edit/asyncmachine-example-negotiation?file=index.ts)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine/tree/master/examples/negotiation)

### Async Dialog

Presents the following concepts: `automatic states`, `synchronous mutations`, `delayed mutations` and loose coupling.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1ede5f62717e0013877cdc)
* [Inspect on StackBlitz](https://stackblitz.com/edit/asyncmachine-example-async-dialog?file=index.ts)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine/tree/master/examples/async-dialog)

### Exception State

A simple fault tolerance (retrying) using the `Exception` state.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1ee7113321180012ebafcf)
* [Inspect on Stackblitz](https://stackblitz.com/edit/asyncmachine-example-exception?file=index.ts)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine/tree/master/examples/exception-state)

### Piping

Shows how `pipes` forward states between machines.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1eea671eaec500126c1be7)
* [Inspect on Stackblitz](https://stackblitz.com/edit/asyncmachine-example-piping?file=index.ts)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine/tree/master/examples/piping)

### Transitions

Shows various types of `transition handlers` and the way params get passed to them.

* [Edit on RunKit](https://runkit.com/tobiaszcudnik/5b1eeaba3b97b60012c83ec0)
* [Inspect on Stackblitz](https://stackblitz.com/edit/asyncmachine-example-transitions?file=index.ts)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine/tree/master/examples/transitions)

### TodoMVC and React

Classic TodoMCV example using **AsyncMachine** as the controller and **React** as the view.

* [Edit on Stackblitz](https://stackblitz.com/edit/asyncmachine-example-todomvc?file=src/controller.js)
* [Source on GitHub](https://github.com/TobiaszCudnik/todomvc-asyncmachine)

### State streams with RxJS

Observe state changes and navigate through specific paths with RxJS, then feed the result back as a state.

* Comming soon!

### Restaurant

A complex example showing how to solve the **producer / consumer problem** using AsyncMachine.

* [Inspect on StackBlitz](https://stackblitz.com/edit/asyncmachine-inspector-restaurant)
* [Source on GitHub](https://github.com/TobiaszCudnik/asyncmachine-inspector/tree/master/examples/restaurant)

[![inspector view](https://raw.githubusercontent.com/TobiaszCudnik/asyncmachine/gh-pages/images/restaurant.png)](https://stackblitz.com/edit/asyncmachine-inspector-restaurant)

### GTD Bot

For a real world example check [GTD Bot](https://github.com/TobiaszCudnik/gtd-bot/tree/master/src) - a sync engine for Google APIs.

[![Preview](http://tobiaszcudnik.github.io/asyncmachine-inspector/sample.png)](http://tobiaszcudnik.github.io/asyncmachine-inspector/sample.mp4)

## Use cases

* state management
* synchronizing async actions
* solving non-linear problems
* fault tolerance
* resource allocation / disposal
* avoiding race conditions
* thread pools
* sync engines

## License

MIT

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