forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-machine.min.js
1 lines (1 loc) · 3.65 KB
/
state-machine.min.js
1
(function(b){var a={VERSION:"2.2.0",Result:{SUCCEEDED:1,NOTRANSITION:2,CANCELLED:3,PENDING:4},Error:{INVALID_TRANSITION:100,PENDING_TRANSITION:200,INVALID_CALLBACK:300},WILDCARD:"*",ASYNC:"async",create:function(h,i){var k=(typeof h.initial=="string")?{state:h.initial}:h.initial;var g=h.terminal||h["final"];var f=i||h.target||{};var m=h.events||[];var j=h.callbacks||{};var d={};var l=function(o){var q=(o.from instanceof Array)?o.from:(o.from?[o.from]:[a.WILDCARD]);d[o.name]=d[o.name]||{};for(var p=0;p<q.length;p++){d[o.name][q[p]]=o.to||q[p]}};if(k){k.event=k.event||"startup";l({name:k.event,from:"none",to:k.state})}for(var e=0;e<m.length;e++){l(m[e])}for(var c in d){if(d.hasOwnProperty(c)){f[c]=a.buildEvent(c,d[c])}}for(var c in j){if(j.hasOwnProperty(c)){f[c]=j[c]}}f.current="none";f.is=function(n){return(n instanceof Array)?(n.indexOf(this.current)>=0):(this.current===n)};f.can=function(n){return !this.transition&&(d[n].hasOwnProperty(this.current)||d[n].hasOwnProperty(a.WILDCARD))};f.cannot=function(n){return !this.can(n)};f.error=h.error||function(p,t,s,o,n,r,q){throw q||r};f.isFinished=function(){return this.is(g)};if(k&&!k.defer){f[k.event]()}return f},doCallback:function(h,f,d,j,i,c){if(f){try{return f.apply(h,[d,j,i].concat(c))}catch(g){return h.error(d,j,i,c,a.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",g)}}},beforeAnyEvent:function(e,d,g,f,c){return a.doCallback(e,e.onbeforeevent,d,g,f,c)},afterAnyEvent:function(e,d,g,f,c){return a.doCallback(e,e.onafterevent||e.onevent,d,g,f,c)},leaveAnyState:function(e,d,g,f,c){return a.doCallback(e,e.onleavestate,d,g,f,c)},enterAnyState:function(e,d,g,f,c){return a.doCallback(e,e.onenterstate||e.onstate,d,g,f,c)},changeState:function(e,d,g,f,c){return a.doCallback(e,e.onchangestate,d,g,f,c)},beforeThisEvent:function(e,d,g,f,c){return a.doCallback(e,e["onbefore"+d],d,g,f,c)},afterThisEvent:function(e,d,g,f,c){return a.doCallback(e,e["onafter"+d]||e["on"+d],d,g,f,c)},leaveThisState:function(e,d,g,f,c){return a.doCallback(e,e["onleave"+g],d,g,f,c)},enterThisState:function(e,d,g,f,c){return a.doCallback(e,e["onenter"+f]||e["on"+f],d,g,f,c)},beforeEvent:function(e,d,g,f,c){if((false===a.beforeThisEvent(e,d,g,f,c))||(false===a.beforeAnyEvent(e,d,g,f,c))){return false}},afterEvent:function(e,d,g,f,c){a.afterThisEvent(e,d,g,f,c);a.afterAnyEvent(e,d,g,f,c)},leaveState:function(g,f,i,h,e){var d=a.leaveThisState(g,f,i,h,e),c=a.leaveAnyState(g,f,i,h,e);if((false===d)||(false===c)){return false}else{if((a.ASYNC===d)||(a.ASYNC===c)){return a.ASYNC}}},enterState:function(e,d,g,f,c){a.enterThisState(e,d,g,f,c);a.enterAnyState(e,d,g,f,c)},buildEvent:function(c,d){return function(){var i=this.current;var h=d[i]||d[a.WILDCARD]||i;var f=Array.prototype.slice.call(arguments);if(this.transition){return this.error(c,i,h,f,a.Error.PENDING_TRANSITION,"event "+c+" inappropriate because previous transition did not complete")}if(this.cannot(c)){return this.error(c,i,h,f,a.Error.INVALID_TRANSITION,"event "+c+" inappropriate in current state "+this.current)}if(false===a.beforeEvent(this,c,i,h,f)){return a.Result.CANCELLED}if(i===h){a.afterEvent(this,c,i,h,f);return a.Result.NOTRANSITION}var g=this;this.transition=function(){g.transition=null;g.current=h;a.enterState(g,c,i,h,f);a.changeState(g,c,i,h,f);a.afterEvent(g,c,i,h,f);return a.Result.SUCCEEDED};this.transition.cancel=function(){g.transition=null;a.afterEvent(g,c,i,h,f)};var e=a.leaveState(this,c,i,h,f);if(false===e){this.transition=null;return a.Result.CANCELLED}else{if(a.ASYNC===e){return a.Result.PENDING}else{if(this.transition){return this.transition()}}}}}};if("function"===typeof define){define(function(c){return a})}else{b.StateMachine=a}}(this));