# synchronous-fsm

> A synchronous FSM that triggers events when entering or leaving a state

Latest version **0.0.3** (published 2014-04-13) · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2014-04-13 |
| First published | 2014-04-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Olivier Scherrer |
| Maintainers | podefr |
| Keywords | state, states, statemachine, machine, fsm, finite, transtion, transitions |

## Links

- npm: https://www.npmjs.com/package/synchronous-fsm
- Repository: git@github.com:flams/synchronous-fsm
- Homepage: https://github.com/flams/synchronous-fsm
- Issues: https://github.com/flams/synchronous-fsm/issues
- npm.io page: https://npm.io/package/synchronous-fsm

## Dependencies (2)

- [to-array](https://npm.io/package/to-array.md) ~0.1.4
- [simple-loop](https://npm.io/package/simple-loop.md) 0.0.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.0.3 (latest) — 2014-04-13
- 0.0.2 — 2014-04-06
- 0.0.1 — 2014-04-06

## README

Synchronous finite state machine
=============

A synchronous FSM that also triggers events when entering or leaving a state.

Installation
============

```bash
npm install synchronous-fsm
```

How to use
==========

Require synchronous-fsm:

```js
var FSM = require("synchronous-fsm");
```

To initialize the statemachine we need to define states and transitions first:

```js
var states = {
  // It has an 'opened' state
  "opened": [
      // That accepts a 'pass' event that will execute the 'pass' action
      ["pass", function onPass(event) {
          passCalled = event;
      // And when done, it will transit to the 'closed' state
      }, "closed"],

      // It also has a 'action' event that doesn't trigger a transition
      ["coin", function onCoin(event) {
          console.log("thanks");
      }]
  ],

  // It also has a 'closed' state
  "closed": [
      // That accepts a 'coin' event that will execute the 'coin' action
      ["coin", function coin(event) {
          coinCalled = event;
      // And when done, it will transit back to the 'opened' state
      }, "opened"]
  ]
};
```

Then we can initialize the state machine in the desired state:

```js
// Initialize in the "opened" state
var fsm = new FSM("opened", states);
```

Trigger a transition:

```js
// Will transit to "closed", executing the onPass handler
fsm.event("pass");

// Could also transit with some data that will be received by the handler
fsm.event("pass", "6km/h", "8:30pm");
```

Get the current state:

```js
fsm.getCurrent(); // opened
```

Can also be directly advanced to a given state:

```js
fsm.advance("closed"); // will move the state machine to this state
```

The stateMachine also triggers an "entry" and an "exit" event when entering and leaving a state. Simply add the handlers like for any other event:

```js
var states = {
  // It has an 'opened' state
  "opened": [
      ...

      // This handler will be called when entering the state
      ["entry", function onEntry(event) {
          console.log("entering state...");
      }],

      // This handler will be called when leaving the state
      ["exit", function onExit(event) {
          console.log("leaving state...");
      }],
  ],

  // It also has a 'closed' state
  "closed": [
      ...
  ]
};
```





LICENSE
=======

MIT

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