Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/appsweet-co/stately-machine

A Finite State Machine using Observables
https://github.com/appsweet-co/stately-machine

finite-state-machine npm npm-module npm-package reactive-programming rxjs typescript

Last synced: about 1 month ago
JSON representation

A Finite State Machine using Observables

Awesome Lists containing this project

README

        


Logo

Stately Machine


A Finite State Machine using Observables


## Playground

See [Stackblitz](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts) for an interactive demo.

## API Docs

See our [documentation website](https://appsweet-co.github.io/stately-machine/) for API docs.

## Error Types

See our [API Docs](https://appsweet-co.github.io/stately-machine/types/StatelyErrorType.html) for details on all errors.

## Quick Start

Install using npm.

```zsh
npm i @appsweet-co/stately-machine
```

Import the class directly into your code.

```ts
import { StatelyMachine } from "@appsweet-co/stately-machine";
```

See [Stackblitz](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts) for details on how to set up a new machine.

## More Info

Stately Machine is a Finite State Machine for TypeScript. It uses [Observables](https://rxjs.dev/) instead of callbacks to manage state events.

Use an Enum to [store states](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts:L13-L18). We recommend [String Enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums) for easy iteration using [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values).

You can also set an [optional context](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts:L22-L27) made of any key-value pairs you need. Update and access the context as needed.

Stately Machine makes no assumptions about what you want to do when an error happens. You must subscribe to the [error observables](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts:L47-L51) to get details about each error.

Stately Machine also makes no assumptions about what you want to do for a successful change in state. You must subscribe to the [success observables](https://stackblitz.com/edit/stately-machine?devtoolsheight=100&file=index.ts:L57-L61) to get details about each state change.