Skip to content

Commit

Permalink
Updated for v1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ifandelse committed Jun 21, 2015
1 parent cadc142 commit d94b0eb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# machina v1.1.1
# machina v1.1.2

## What is it?
Machina.js is a JavaScript framework for highly customizable finite state machines (FSMs). Many of the ideas for machina have been *loosely* inspired by the Erlang/OTP FSM behaviors.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "machina",
"version": "1.1.1",
"version": "1.1.2",
"main": [
"lib/machina.js"
],
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v1.1.2

* Internal improvements to the `Fsm` prototype, thanks to @igncp.

### v1.1.1

* Added `compositeState` method to the `BehavioralFsm` and `Fsm` prototypes.
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "machina.js",
"version": "1.1.1",
"version": "1.1.2",
"main": "lib/machina.js",
"scripts": [
"lib/machina.js"
Expand Down
92 changes: 41 additions & 51 deletions lib/machina.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* machina - A library for creating powerful and flexible finite state machines. Loosely inspired by Erlang/OTP's gen_fsm behavior.
* Author: Jim Cowart (http://ifandelse.com)
* Version: v1.1.1
* Version: v1.1.2
* Url: http://machina-js.org/
* License(s): MIT, GPL
*/


( function( root, factory ) {
/* istanbul ignore if */
if ( typeof define === "function" && define.amd ) {
Expand All @@ -23,19 +22,17 @@
root.machina = factory( root._, root );
}
}( this, function( _, global, undefined ) {


var slice = [].slice;
var NEXT_TRANSITION = "transition";
var HANDLING = "handling";
var HANDLED = "handled";
var NO_HANDLER = "nohandler";
var TRANSITION = "transition";
var INVALID_STATE = "invalidstate";
var DEFERRED = "deferred";
var NEW_FSM = "newfsm";

function getDefaultBehavioralOptions() {
var slice = [].slice;
var NEXT_TRANSITION = "transition";
var HANDLING = "handling";
var HANDLED = "handled";
var NO_HANDLER = "nohandler";
var TRANSITION = "transition";
var INVALID_STATE = "invalidstate";
var DEFERRED = "deferred";
var NEW_FSM = "newfsm";

function getDefaultBehavioralOptions() {
return {
initialState: "uninitialized",
eventListeners: {
Expand All @@ -49,7 +46,7 @@ function getDefaultBehavioralOptions() {
};
}

function getDefaultClientMeta() {
function getDefaultClientMeta() {
return {
inputQueue: [],
targetReplayState: "",
Expand All @@ -62,15 +59,15 @@ function getDefaultClientMeta() {
};
}

function getLeaklessArgs( args, startIdx ) {
function getLeaklessArgs( args, startIdx ) {
var result = [];
for ( var i = ( startIdx || 0 ); i < args.length; i++ ) {
result[ i ] = args[ i ];
}
return result;
}

function getChildFsmInstance( config ) {
function getChildFsmInstance( config ) {
if ( !config ) {
return;
}
Expand All @@ -92,7 +89,7 @@ function getChildFsmInstance( config ) {
return childFsmDefinition;
}

function listenToChild( fsm, child ) {
function listenToChild( fsm, child ) {
return child.on( "*", function( eventName, data ) {
switch ( eventName ) {
case "nohandler":
Expand All @@ -119,11 +116,11 @@ function listenToChild( fsm, child ) {
} );
}

// _machKeys are members we want to track across the prototype chain of an extended FSM constructor
// Since we want to eventually merge the aggregate of those values onto the instance so that FSMs
// that share the same extended prototype won't share state *on* those prototypes.
var _machKeys = [ "states", "initialState" ];
var extend = function( protoProps, staticProps ) {
// _machKeys are members we want to track across the prototype chain of an extended FSM constructor
// Since we want to eventually merge the aggregate of those values onto the instance so that FSMs
// that share the same extended prototype won't share state *on* those prototypes.
var _machKeys = [ "states", "initialState" ];
var extend = function( protoProps, staticProps ) {
var parent = this;
var fsm; // placeholder for instance constructor
var machObj = {}; // object used to hold initialState & states from prototype for instance-level merging
Expand Down Expand Up @@ -184,7 +181,7 @@ var extend = function( protoProps, staticProps ) {
return fsm;
};

function createUUID() {
function createUUID() {
var s = [];
var hexDigits = "0123456789abcdef";
for ( var i = 0; i < 36; i++ ) {
Expand All @@ -198,7 +195,7 @@ function createUUID() {
return s.join( "" );
}

var utils = {
var utils = {
makeFsmNamespace: ( function() {
var machinaCount = 0;
return function() {
Expand All @@ -212,11 +209,9 @@ var utils = {
createUUID: createUUID
};



var emitter = {
var emitter = {

emit: function( eventName ) {
emit: function( eventName ) {
var args = getLeaklessArgs( arguments );
if ( this.eventListeners[ "*" ] ) {
_.each( this.eventListeners[ "*" ], function( callback ) {
Expand Down Expand Up @@ -252,7 +247,7 @@ var emitter = {
}
},

on: function( eventName, callback ) {
on: function( eventName, callback ) {
var self = this;
self.eventListeners = self.eventListeners || { "*": [] };
if ( !self.eventListeners[ eventName ] ) {
Expand All @@ -268,7 +263,7 @@ var emitter = {
};
},

off: function( eventName, callback ) {
off: function( eventName, callback ) {
this.eventListeners = this.eventListeners || { "*": [] };
if ( !eventName ) {
this.eventListeners = {};
Expand All @@ -280,19 +275,18 @@ var emitter = {
}
}
}
};
};


var MACHINA_PROP = "__machina__";
var MACHINA_PROP = "__machina__";

function BehavioralFsm( options ) {
function BehavioralFsm( options ) {
_.extend( this, options );
_.defaults( this, getDefaultBehavioralOptions() );
this.initialize.apply( this, arguments );
machina.emit( NEW_FSM, this );
}

_.extend( BehavioralFsm.prototype, {
_.extend( BehavioralFsm.prototype, {
initialize: function() {},

initClient: function initClient( client ) {
Expand Down Expand Up @@ -511,11 +505,9 @@ _.extend( BehavioralFsm.prototype, {
}
}, emitter );

BehavioralFsm.extend = extend;


BehavioralFsm.extend = extend;

var Fsm = {
var Fsm = {
constructor: function() {
BehavioralFsm.apply( this, arguments );
this.ensureClientMeta();
Expand Down Expand Up @@ -576,24 +568,22 @@ var Fsm = {
}
};

_.each( [
"handle",
"transition",
"deferUntilTransition",
"processQueue",
"clearQueue"
_.each( [
"handle",
"transition",
"deferUntilTransition",
"processQueue",
"clearQueue"
], function( methodWithClientInjected ) {
Fsm[methodWithClientInjected] = function() {
var args = this.ensureClientArg( utils.getLeaklessArgs( arguments ) );
return BehavioralFsm.prototype[methodWithClientInjected].apply( this, args );
};
} );

Fsm = BehavioralFsm.extend( Fsm );


Fsm = BehavioralFsm.extend( Fsm );

var machina = _.merge( emitter, {
var machina = _.merge( emitter, {
Fsm: Fsm,
BehavioralFsm: BehavioralFsm,
utils: utils,
Expand Down
2 changes: 1 addition & 1 deletion lib/machina.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "machina",
"description": "A library for creating powerful and flexible finite state machines. Loosely inspired by Erlang/OTP's gen_fsm behavior.",
"version": "1.1.1",
"version": "1.1.2",
"homepage": "http://machina-js.org/",
"repository": {
"type": "git",
Expand Down Expand Up @@ -101,7 +101,7 @@
"devDependencies": {
"bower": "1.x",
"express": "~3.4.7",
"gulp": "~3.8.11",
"gulp": "~3.9.0",
"gulp-changed": "^1.2.1",
"gulp-header": "~1.2.2",
"gulp-hint-not": "~0.0.3",
Expand All @@ -110,14 +110,14 @@
"gulp-jshint": "^1.11.0",
"gulp-plato": "~1.0.2",
"gulp-rename": "~1.2.2",
"gulp-spawn-mocha": "^2.0.1",
"gulp-spawn-mocha": "^2.2.1",
"gulp-uglify": "~1.2.0",
"gulp-util": "~3.0.4",
"jshint-stylish": "^2.0.0",
"jshint-stylish": "^2.0.1",
"mocha": "^2.2.5",
"open": "~0.0.4",
"should": "^6.0.3",
"sinon": "1.14.1"
"sinon": "~1.15.3"
},
"licenses": [
{
Expand Down

0 comments on commit d94b0eb

Please sign in to comment.