Skip to content
PicoMachine: minimal finite state machine
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.travis.yml
Makefile
README.md
package.json

README.md

PicoMachine

a.k.a.: MicroMachine for javascript

Build Status

Description

PicoMachine is a javascript version of MicroMachine and have the same goals. To be a minimal and fully functional minimal state machine.

Usage

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

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');
});
Something went wrong with that request. Please try again.