https://github.com/shime/micro-machine
Minimal state machine.
https://github.com/shime/micro-machine
Last synced: 9 months ago
JSON representation
Minimal state machine.
- Host: GitHub
- URL: https://github.com/shime/micro-machine
- Owner: shime
- License: unlicense
- Created: 2014-01-03T10:10:42.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-22T10:18:52.000Z (over 11 years ago)
- Last Synced: 2024-04-14T07:52:31.592Z (about 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 301 KB
- Stars: 9
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Micro Machine
[](https://travis-ci.org/shime/micro-machine)
Minimal state machine implementation.
Heavily inspired by [soveran/micromachine](https://github.com/soveran/micromachine).
### Installation
```shell
npm install micro-machine
```
### Usage
```javascript
var Machine = require('micro-machine')
, machine = new Machine('pending')
machine.transitionsFor.confirm = { pending: 'confirmed' }
machine.transitionsFor.reset = { confirmed: 'pending' }
machine.trigger('confirm')
console.log(machine.state) // 'confirmed'
machine.trigger('reset')
console.log(machine.state) // 'pending'
```
### Callbacks
You can also define callbacks that will be invoked after the specified transition.
```javascript
var Machine = require('micro-machine')
, machine = new Machine('pending')
machine.transitionsFor.confirm = { pending: 'confirmed' }
machine.transitionsFor.reset = { confirmed: 'pending' }
var state
/* Use 'any' to define callback for any transition. */
machine.on('any', function(machine){
state = machine.state
})
machine.on('reset', function() { console.log('resetting...') })
machine.trigger('confirm')
console.log(state) // 'confirmed'
machine.trigger('reset') // 'resetting...'
```
### Development
Run tests with
npm test
or build it with
npm run build
### Unlicense
This repository and its contents belong to the public domain.
It has been released under the [UNLICENSE](https://github.com/shime/micro-machine/blob/master/UNLICENSE).