javascript-state-machine
Advanced tools
Comparing version 2.3.2 to 2.3.4
{ | ||
"name": "javascript-state-machine", | ||
"version": "2.3.2", | ||
"version": "2.3.4", | ||
"homepage": "https://github.com/jakesgordon/javascript-state-machine", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "2.3.2" | ||
"version": "2.3.4" | ||
} |
@@ -1,2 +0,2 @@ | ||
Javascript Finite State Machine (v2.3.2) | ||
Javascript Finite State Machine (v2.3.4) | ||
======================================== | ||
@@ -10,2 +10,7 @@ | ||
This library has also been ported to: | ||
* [Go](https://github.com/looplab/fsm) by @maxpersson | ||
* [Python](https://github.com/oxplot/fysom) by @oxplot | ||
Download | ||
@@ -54,6 +59,7 @@ ======== | ||
* fsm.current - contains the current state | ||
* fsm.is(s) - return true if state `s` is the current state | ||
* fsm.can(e) - return true if event `e` can be fired in the current state | ||
* fsm.cannot(e) - return true if event `e` cannot be fired in the current state | ||
* fsm.current - contains the current state | ||
* fsm.is(s) - return true if state `s` is the current state | ||
* fsm.can(e) - return true if event `e` can be fired in the current state | ||
* fsm.cannot(e) - return true if event `e` cannot be fired in the current state | ||
* fsm.transitions() - return list of events that are allowed from the current state | ||
@@ -267,3 +273,3 @@ Multiple 'from' and 'to' states for a single event | ||
By default, if you dont specify any initial state, the state machine will be in the `'none'` | ||
By default, if you don't specify any initial state, the state machine will be in the `'none'` | ||
state and you would need to provide an event to take it out of this state: | ||
@@ -270,0 +276,0 @@ |
@@ -0,1 +1,11 @@ | ||
Version 2.3.4 (January 17 2014) | ||
------------------------------- | ||
* helper method to list which events are allowed from the current state (issue #71 - thanks to @mgoldsborough and @chopj) | ||
Version 2.3.3 (October 17 2014) | ||
------------------------------- | ||
* added web worker compatability (issue #65 - thanks to @offirmo) | ||
Version 2.3.2 (March 16 2014) | ||
@@ -2,0 +12,0 @@ ----------------------------- |
@@ -5,3 +5,3 @@ /* | ||
Copyright (c) 2012, 2013, 2014, Jake Gordon and contributors | ||
Copyright (c) 2012, 2013, 2014, 2015, Jake Gordon and contributors | ||
Released under the MIT license - https://github.com/jakesgordon/javascript-state-machine/blob/master/LICENSE | ||
@@ -17,3 +17,3 @@ | ||
VERSION: "2.3.2", | ||
VERSION: "2.3.4", | ||
@@ -42,8 +42,9 @@ //--------------------------------------------------------------------------- | ||
var initial = (typeof cfg.initial == 'string') ? { state: cfg.initial } : cfg.initial; // allow for a simple string, or an object with { state: 'foo', event: 'setup', defer: true|false } | ||
var terminal = cfg.terminal || cfg['final']; | ||
var fsm = target || cfg.target || {}; | ||
var events = cfg.events || []; | ||
var callbacks = cfg.callbacks || {}; | ||
var map = {}; | ||
var initial = (typeof cfg.initial == 'string') ? { state: cfg.initial } : cfg.initial; // allow for a simple string, or an object with { state: 'foo', event: 'setup', defer: true|false } | ||
var terminal = cfg.terminal || cfg['final']; | ||
var fsm = target || cfg.target || {}; | ||
var events = cfg.events || []; | ||
var callbacks = cfg.callbacks || {}; | ||
var map = {}; // track state transitions allowed for an event { event: { from: [ to ] } } | ||
var transitions = {}; // track events allowed from a state { state: [ event ] } | ||
@@ -53,4 +54,8 @@ var add = function(e) { | ||
map[e.name] = map[e.name] || {}; | ||
for (var n = 0 ; n < from.length ; n++) | ||
for (var n = 0 ; n < from.length ; n++) { | ||
transitions[e.from] = transitions[e.from] || []; | ||
transitions[e.from].push(e.name); | ||
map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified | ||
} | ||
}; | ||
@@ -76,10 +81,10 @@ | ||
fsm.current = 'none'; | ||
fsm.is = function(state) { return (state instanceof Array) ? (state.indexOf(this.current) >= 0) : (this.current === state); }; | ||
fsm.can = function(event) { return !this.transition && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); } | ||
fsm.cannot = function(event) { return !this.can(event); }; | ||
fsm.error = cfg.error || function(name, from, to, args, error, msg, e) { throw e || msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3 and #17) | ||
fsm.current = 'none'; | ||
fsm.is = function(state) { return (state instanceof Array) ? (state.indexOf(this.current) >= 0) : (this.current === state); }; | ||
fsm.can = function(event) { return !this.transition && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); } | ||
fsm.cannot = function(event) { return !this.can(event); }; | ||
fsm.transitions = function() { return transitions[this.current]; }; | ||
fsm.isFinished = function() { return this.is(terminal); }; | ||
fsm.error = cfg.error || function(name, from, to, args, error, msg, e) { throw e || msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3 and #17) | ||
fsm.isFinished = function() { return this.is(terminal); }; | ||
if (initial && !initial.defer) | ||
@@ -217,7 +222,12 @@ fsm[initial.event](); | ||
//======== | ||
else if (window) { | ||
else if (typeof window !== 'undefined') { | ||
window.StateMachine = StateMachine; | ||
} | ||
//=========== | ||
// WEB WORKER | ||
//=========== | ||
else if (typeof self !== 'undefined') { | ||
self.StateMachine = StateMachine; | ||
} | ||
}()); | ||
@@ -1,1 +0,1 @@ | ||
(function(){var a={VERSION:"2.3.2",Result:{SUCCEEDED:1,NOTRANSITION:2,CANCELLED:3,PENDING:4},Error:{INVALID_TRANSITION:100,PENDING_TRANSITION:200,INVALID_CALLBACK:300},WILDCARD:"*",ASYNC:"async",create:function(g,h){var j=(typeof g.initial=="string")?{state:g.initial}:g.initial;var f=g.terminal||g["final"];var e=h||g.target||{};var l=g.events||[];var i=g.callbacks||{};var c={};var k=function(m){var p=(m.from instanceof Array)?m.from:(m.from?[m.from]:[a.WILDCARD]);c[m.name]=c[m.name]||{};for(var o=0;o<p.length;o++){c[m.name][p[o]]=m.to||p[o]}};if(j){j.event=j.event||"startup";k({name:j.event,from:"none",to:j.state})}for(var d=0;d<l.length;d++){k(l[d])}for(var b in c){if(c.hasOwnProperty(b)){e[b]=a.buildEvent(b,c[b])}}for(var b in i){if(i.hasOwnProperty(b)){e[b]=i[b]}}e.current="none";e.is=function(m){return(m instanceof Array)?(m.indexOf(this.current)>=0):(this.current===m)};e.can=function(m){return !this.transition&&(c[m].hasOwnProperty(this.current)||c[m].hasOwnProperty(a.WILDCARD))};e.cannot=function(m){return !this.can(m)};e.error=g.error||function(o,s,r,n,m,q,p){throw p||q};e.isFinished=function(){return this.is(f)};if(j&&!j.defer){e[j.event]()}return e},doCallback:function(g,d,c,i,h,b){if(d){try{return d.apply(g,[c,i,h].concat(b))}catch(f){return g.error(c,i,h,b,a.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",f)}}},beforeAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onbeforeevent,c,f,e,b)},afterAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onafterevent||d.onevent,c,f,e,b)},leaveAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onleavestate,c,f,e,b)},enterAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onenterstate||d.onstate,c,f,e,b)},changeState:function(d,c,f,e,b){return a.doCallback(d,d.onchangestate,c,f,e,b)},beforeThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onbefore"+c],c,f,e,b)},afterThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onafter"+c]||d["on"+c],c,f,e,b)},leaveThisState:function(d,c,f,e,b){return a.doCallback(d,d["onleave"+f],c,f,e,b)},enterThisState:function(d,c,f,e,b){return a.doCallback(d,d["onenter"+e]||d["on"+e],c,f,e,b)},beforeEvent:function(d,c,f,e,b){if((false===a.beforeThisEvent(d,c,f,e,b))||(false===a.beforeAnyEvent(d,c,f,e,b))){return false}},afterEvent:function(d,c,f,e,b){a.afterThisEvent(d,c,f,e,b);a.afterAnyEvent(d,c,f,e,b)},leaveState:function(f,e,h,g,d){var c=a.leaveThisState(f,e,h,g,d),b=a.leaveAnyState(f,e,h,g,d);if((false===c)||(false===b)){return false}else{if((a.ASYNC===c)||(a.ASYNC===b)){return a.ASYNC}}},enterState:function(d,c,f,e,b){a.enterThisState(d,c,f,e,b);a.enterAnyState(d,c,f,e,b)},buildEvent:function(b,c){return function(){var h=this.current;var g=c[h]||c[a.WILDCARD]||h;var e=Array.prototype.slice.call(arguments);if(this.transition){return this.error(b,h,g,e,a.Error.PENDING_TRANSITION,"event "+b+" inappropriate because previous transition did not complete")}if(this.cannot(b)){return this.error(b,h,g,e,a.Error.INVALID_TRANSITION,"event "+b+" inappropriate in current state "+this.current)}if(false===a.beforeEvent(this,b,h,g,e)){return a.Result.CANCELLED}if(h===g){a.afterEvent(this,b,h,g,e);return a.Result.NOTRANSITION}var f=this;this.transition=function(){f.transition=null;f.current=g;a.enterState(f,b,h,g,e);a.changeState(f,b,h,g,e);a.afterEvent(f,b,h,g,e);return a.Result.SUCCEEDED};this.transition.cancel=function(){f.transition=null;a.afterEvent(f,b,h,g,e)};var d=a.leaveState(this,b,h,g,e);if(false===d){this.transition=null;return a.Result.CANCELLED}else{if(a.ASYNC===d){return a.Result.PENDING}else{if(this.transition){return this.transition()}}}}}};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=a}exports.StateMachine=a}else{if(typeof define==="function"&&define.amd){define(function(b){return a})}else{if(window){window.StateMachine=a}}}}()); | ||
(function(){var a={VERSION:"2.3.4",Result:{SUCCEEDED:1,NOTRANSITION:2,CANCELLED:3,PENDING:4},Error:{INVALID_TRANSITION:100,PENDING_TRANSITION:200,INVALID_CALLBACK:300},WILDCARD:"*",ASYNC:"async",create:function(g,h){var j=(typeof g.initial=="string")?{state:g.initial}:g.initial;var f=g.terminal||g["final"];var e=h||g.target||{};var m=g.events||[];var i=g.callbacks||{};var c={};var k={};var l=function(o){var q=(o.from instanceof Array)?o.from:(o.from?[o.from]:[a.WILDCARD]);c[o.name]=c[o.name]||{};for(var p=0;p<q.length;p++){k[o.from]=k[o.from]||[];k[o.from].push(o.name);c[o.name][q[p]]=o.to||q[p]}};if(j){j.event=j.event||"startup";l({name:j.event,from:"none",to:j.state})}for(var d=0;d<m.length;d++){l(m[d])}for(var b in c){if(c.hasOwnProperty(b)){e[b]=a.buildEvent(b,c[b])}}for(var b in i){if(i.hasOwnProperty(b)){e[b]=i[b]}}e.current="none";e.is=function(n){return(n instanceof Array)?(n.indexOf(this.current)>=0):(this.current===n)};e.can=function(n){return !this.transition&&(c[n].hasOwnProperty(this.current)||c[n].hasOwnProperty(a.WILDCARD))};e.cannot=function(n){return !this.can(n)};e.transitions=function(){return k[this.current]};e.isFinished=function(){return this.is(f)};e.error=g.error||function(p,t,s,o,n,r,q){throw q||r};if(j&&!j.defer){e[j.event]()}return e},doCallback:function(g,d,c,i,h,b){if(d){try{return d.apply(g,[c,i,h].concat(b))}catch(f){return g.error(c,i,h,b,a.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",f)}}},beforeAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onbeforeevent,c,f,e,b)},afterAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onafterevent||d.onevent,c,f,e,b)},leaveAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onleavestate,c,f,e,b)},enterAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onenterstate||d.onstate,c,f,e,b)},changeState:function(d,c,f,e,b){return a.doCallback(d,d.onchangestate,c,f,e,b)},beforeThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onbefore"+c],c,f,e,b)},afterThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onafter"+c]||d["on"+c],c,f,e,b)},leaveThisState:function(d,c,f,e,b){return a.doCallback(d,d["onleave"+f],c,f,e,b)},enterThisState:function(d,c,f,e,b){return a.doCallback(d,d["onenter"+e]||d["on"+e],c,f,e,b)},beforeEvent:function(d,c,f,e,b){if((false===a.beforeThisEvent(d,c,f,e,b))||(false===a.beforeAnyEvent(d,c,f,e,b))){return false}},afterEvent:function(d,c,f,e,b){a.afterThisEvent(d,c,f,e,b);a.afterAnyEvent(d,c,f,e,b)},leaveState:function(f,e,h,g,d){var c=a.leaveThisState(f,e,h,g,d),b=a.leaveAnyState(f,e,h,g,d);if((false===c)||(false===b)){return false}else{if((a.ASYNC===c)||(a.ASYNC===b)){return a.ASYNC}}},enterState:function(d,c,f,e,b){a.enterThisState(d,c,f,e,b);a.enterAnyState(d,c,f,e,b)},buildEvent:function(b,c){return function(){var h=this.current;var g=c[h]||c[a.WILDCARD]||h;var e=Array.prototype.slice.call(arguments);if(this.transition){return this.error(b,h,g,e,a.Error.PENDING_TRANSITION,"event "+b+" inappropriate because previous transition did not complete")}if(this.cannot(b)){return this.error(b,h,g,e,a.Error.INVALID_TRANSITION,"event "+b+" inappropriate in current state "+this.current)}if(false===a.beforeEvent(this,b,h,g,e)){return a.Result.CANCELLED}if(h===g){a.afterEvent(this,b,h,g,e);return a.Result.NOTRANSITION}var f=this;this.transition=function(){f.transition=null;f.current=g;a.enterState(f,b,h,g,e);a.changeState(f,b,h,g,e);a.afterEvent(f,b,h,g,e);return a.Result.SUCCEEDED};this.transition.cancel=function(){f.transition=null;a.afterEvent(f,b,h,g,e)};var d=a.leaveState(this,b,h,g,e);if(false===d){this.transition=null;return a.Result.CANCELLED}else{if(a.ASYNC===d){return a.Result.PENDING}else{if(this.transition){return this.transition()}}}}}};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=a}exports.StateMachine=a}else{if(typeof define==="function"&&define.amd){define(function(b){return a})}else{if(typeof window!=="undefined"){window.StateMachine=a}else{if(typeof self!=="undefined"){self.StateMachine=a}}}}}()); |
@@ -212,3 +212,3 @@ //----------------------------------------------------------------------------- | ||
myFSM = function() { | ||
var myFSM = function() { | ||
this.called = []; | ||
@@ -215,0 +215,0 @@ this.startup(); |
@@ -114,2 +114,35 @@ //----------------------------------------------------------------------------- | ||
test("transitions", function() { | ||
var fsm = StateMachine.create({ | ||
initial: 'green', | ||
events: [ | ||
{ name: 'warn', from: 'green', to: 'yellow' }, | ||
{ name: 'panic', from: 'yellow', to: 'red' }, | ||
{ name: 'calm', from: 'red', to: 'yellow' }, | ||
{ name: 'clear', from: 'yellow', to: 'green' } | ||
]}); | ||
equal(fsm.current, 'green', 'current state should be yellow'); | ||
deepEqual(fsm.transitions(), ['warn'], 'current transition(s) should be yellow'); | ||
fsm.warn(); | ||
equal(fsm.current, 'yellow', 'current state should be yellow'); | ||
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transition(s) should be panic and clear'); | ||
fsm.panic(); | ||
equal(fsm.current, 'red', 'current state should be red'); | ||
deepEqual(fsm.transitions(), ['calm'], 'current transition(s) should be calm'); | ||
fsm.calm(); | ||
equal(fsm.current, 'yellow', 'current state should be yellow'); | ||
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transion(s) should be panic and clear'); | ||
fsm.clear(); | ||
equal(fsm.current, 'green', 'current state should be green'); | ||
deepEqual(fsm.transitions(), ['warn'], 'current transion(s) should be warn'); | ||
}); | ||
//----------------------------------------------------------------------------- | ||
test("isFinished", function() { | ||
@@ -116,0 +149,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1092250
3764
368