MicroMachineJS - a minimal state machine for JavaScript

I've published a simple JavaScript library the other day. Its goal is to help dealing
with states and it provides a simple API for managing a minimalistic implementation of a state machine.

It's really miniature - it has only 50 lines of code. Its source code should be easy to read, even to a beginner. I've used Jasmine and Grunt to TDD it from scratch, so hopefully - the tests should also be really expressive.

It has no dependencies and supports everything a state machine should have, nothing more.

Installation

Examples

Here's a walkthrough of its features.

// create a new instance and set its initial state to "pending"varmachine=newMicroMachine("pending");console.log(machine.state);// outputs 'pending'// define transitionsmachine.transitionsFor.confirm={pending:'confirmed'};machine.transitionsFor.ignore={pending:'ignored'};machine.transitionsFor.reset={confirmed:'pending',ignored:'pending'};machine.trigger('confirm');console.log(machine.state);// outputs 'confirmed'// define callbacksmachine.on('reset',function(){console.log("resetting...");});machine.trigger('reset');// resets state and outputs 'resetting...'// define special callback that will be called after any transitionmachine.on('any',function(machine){console.log("transitioned to '"+machine.state+"'");});machine.trigger('ignore');// outputs "transitioned to 'ignored'"

Composition capabilities

Adding MicroMachine to your objects is piece of cake - just use composition!