# fsmjs

> State machines are back

Latest version **0.2.1** (published 2012-07-18) · MIT license · 0 weekly downloads

## Install

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

## 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.2.1 |
| Published | 2012-07-18 |
| First published | 2012-01-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.6.6 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Elad Ben-Israel |
| Maintainers | eladb, yosefd |
| Keywords | fsm, state, async, state machine |

## Links

- npm: https://www.npmjs.com/package/fsmjs
- Repository: https://github.com/eladb/node-fsmjs
- npm.io page: https://npm.io/package/fsmjs

## 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.2.1 (latest) — 2012-07-18
- 0.2.0 — 2012-01-04
- 0.1.0 — 2012-01-02

## README

# fsmjs #

[![Build Status](https://secure.travis-ci.org/anodejs/node-fsmjs.png)](http://travis-ci.org/anodejs/node-fsmjs)

State machines are back, the async way!

```bash
$ npm install fsmjs
```

## By example ##

```js
var fsmjs = require('../lib/fsmjs');

var tim = fsmjs({

	idle: {

		// when 'go' or 'start' or 'g' are pushed, we move to 'running' and start
		// an interval that emits a 'tick' event every 100ms.
		'(go|start|g)': function(cb) {
			process.stdout.write('starting engines...\n');
			tim.state = 'running'; 
			tim._timer = tim.interval('tick', 100);
			cb();
		},

		// strings are target states (and emitted events)
		exit: 'end',
		e: 'error',

		// any other event in this state shows this error
		'.*': function(cb, e) {
			console.log('error: i cant understand what you mean by "' + e + '"');
			cb();
		},
	},

	running: {

		// animate clock every tick
		tick: function(cb) {
			var clock = [ '|', '/', '-', '\\' ];
			process.stdout.write('(' + clock[tim._i] + ")");
			for (var i = 0; i < 50; ++i) process.stdout.write(' ');
			process.stdout.write('\r');
			tim._i = (tim._i + 1) % clock.length;
			cb();
		},

		// cannot exit from this state
		exit: 'error',

		// when 'no' or 'stop' or 'x' are pushed, move to 'stopping' and start
		// a 2sec timeout that emits 'elapsed' when elapsed (surpise!)
		'(no|stop|x)': function(cb) { 
			process.stdout.write('stopping...\n');
			tim.state = 'stopping';
			tim.timeout('stopped', 5000);

			cb();
		},

		$exit: function(cb) {
			// take some time before changing state.
			console.log('our running times are over.. give me 2 more seconds.');
			setTimeout(cb, 2000);
		},

	},

	stopping: {

		// called when the stopping timer elapses. clears the 
		// interval and changes state to 'idle'
		stopped: function(cb) {
			process.stdout.write('\nall done.\n');
			clearInterval(tim._timer);
			tim.state = 'idle';
			cb();
		},

		// a tick during stop operation, show dots
		tick: function(cb) {
			process.stdout.write(".");
			cb();
		},

		exit: 'error',

	},

	error: function(cb, state) {
		console.log('An error occured in state', state);
		tim.state = state;
		cb();
	},

	_timer: null, // timer object to allow clearing the interval
	_i: 0, // animated clock
});

tim.on('end', function() {
	process.exit();
});

tim.on('error', function() {
	console.log('on-error');
});

tim.on('idle.start', function() {
	console.log("try 'go' the next time...");
});

var i = require('../lib/debugger')(tim, { verbose: false });
```

More examples under `examples/`.

## Lisence

MIT

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