# extra-fsm

> The library supports both OOP and FP.

Latest version **0.2.1** (published 2023-06-10) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2023-06-10 |
| First published | 2022-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 3 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | BlackGlory |
| Maintainers | black_glory |

## Links

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

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.0
- [extra-utils](https://npm.io/package/extra-utils.md) ^5.1.1
- [@blackglory/errors](https://npm.io/package/@blackglory/errors.md) ^3.0.2

## Recent versions

- 0.2.1 (latest) — 2023-06-10
- 0.2.0 — 2023-04-05
- 0.1.1 — 2022-10-24
- 0.1.0 — 2022-09-24

## README

# extra-fsm
The library supports both OOP and FP.

## Install
```sh
npm install --save extra-fsm
# or
yarn add extra-fsm
```

### Usage
```ts
import { FiniteStateMachine, transition } from 'extra-fsm'

const schema = {
  stopped: { start: 'running' }
, running: { stop: 'stopped' }
}

// OOP
const fsm = new FiniteStateMachine(schema, 'stopped')
fsm.send('start')
console.log(fsm.state) // running

// FP
const newState = transition(schema, 'stopped', 'start')
console.log(newState)// running
```

## API
```ts
type IFiniteStateMachineSchema<
  State extends string | number | symbol
, Event extends string | number | symbol
> = Record<State, Partial<Record<Event, State>>>
```

### FiniteStateMachine
```ts
class FiniteStateMachine<
  State extends string | number | symbol
, Event extends string | number | symbol
> {
  get [Symbol.toStringTag](): string
  get state(): State

  constructor(
    schema: IFiniteStateMachineSchema<State, Event>
  , initialState: State
  )

  matches(state: State): boolean
  can(event: Event): boolean

  /**
   * @throws {BadEventError}
   */
  send(event: Event): void
}
```

### ObservableFiniteStateMachine
```ts
interface IFiniteStateMachineStateChange<
  State extends string | number | symbol
, Event extends string | number | symbol
> {
  event: Event
  oldState: State
  newState: State
}

class ObservableFiniteStateMachine<
  State extends string | number | symbol
, Event extends string | number | symbol
> extends FiniteStateMachine<State, Event> {
  get [Symbol.toStringTag](): string

  observeStateChanges(): Observable<IFiniteStateMachineStateChange<State, Event>>
}
```

### transition
```ts
export function transition<
  State extends string | number | symbol
, Event extends string | number | symbol
, TransitionEvent extends Event
>(
  schema: IFiniteStateMachineSchema<State, Event>
, state: State
, event: TransitionEvent
): State
```

### canTransition
```ts
function canTransition<
  State extends string | number | symbol
, Event extends string | number | symbol
>(
  schema: IFiniteStateMachineSchema<State, Event>
, state: State
, event: Event
) => boolean
```

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