Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elcuervo/picomachine
PicoMachine: minimal finite state machine
https://github.com/elcuervo/picomachine
Last synced: 3 months ago
JSON representation
PicoMachine: minimal finite state machine
- Host: GitHub
- URL: https://github.com/elcuervo/picomachine
- Owner: elcuervo
- Created: 2011-12-20T21:22:45.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-03-09T13:41:04.000Z (almost 13 years ago)
- Last Synced: 2023-04-10T15:48:46.317Z (almost 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 97.7 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PicoMachine
_a.k.a.: [MicroMachine](https://github.com/soveran/micromachine) for javascript_[![Build Status](https://secure.travis-ci.org/elcuervo/picomachine.png?branch=master)](http://travis-ci.org/elcuervo/picomachine)
## Description
PicoMachine is a javascript version of [MicroMachine](https://github.com/soveran/micromachine) and have the same goals.
To be a minimal and fully functional minimal state machine.## Usage
```javascript
var PicoMachine = require('picomachine');var machine = new PicoMachine('new'); // Initial state.
machine.transitionsFor['confirm'] = { new: 'confirmed' };
machine.transitionsFor['ignore'] = { new: 'ignored' };
machine.transitionsFor['reset'] = { confirmed: 'new', ignored: 'new' };machine.trigger('confirm'); // true
machine.trigger('ignore'); // false
machine.trigger('reset'); // true
machine.trigger('ignore'); // true
```## Callbacks
```javascript
machine.trigger('reset');// Callback for the 'confirm' event
machine.on('confirmed', function() {
console.log('The thing is confirmed');
});// Callback for all the things!
machine.on('any', function() {
console.log("I'm triggered in allllll the events!!!");
});machine.trigger('confirm');
// The thing is confirmed
// I'm triggered in allllll the events!!!machine.on('something', function() {
// Inside a callback 'this' represents the machine
// so you can trigger other events within it
this.trigger('otherThing');
});
```