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.93 KB
/
state-machine.min.js
1
!function(){var t={VERSION:"2.4.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(e,n){var r="string"==typeof e.initial?{state:e.initial}:e.initial,i=e.terminal||e.final,a=n||e.target||{},o=e.events||[],u=e.callbacks||{},s={},c={},f=function(e){var n=Array.isArray(e.from)?e.from:e.from?[e.from]:[t.WILDCARD];s[e.name]=s[e.name]||{};for(var r=0;r<n.length;r++)c[n[r]]=c[n[r]]||[],c[n[r]].push(e.name),s[e.name][n[r]]=e.to||n[r];e.to&&(c[e.to]=c[e.to]||[])};r&&(r.event=r.event||"startup",f({name:r.event,from:"none",to:r.state}));for(var l=0;l<o.length;l++)f(o[l]);for(var v in s)s.hasOwnProperty(v)&&(a[v]=t.buildEvent(v,s[v]));for(var v in u)u.hasOwnProperty(v)&&(a[v]=u[v]);return a.current="none",a.is=function(t){return Array.isArray(t)?t.indexOf(this.current)>=0:this.current===t},a.can=function(e){return!this.transition&&void 0!==s[e]&&(s[e].hasOwnProperty(this.current)||s[e].hasOwnProperty(t.WILDCARD))},a.cannot=function(t){return!this.can(t)},a.transitions=function(){return(c[this.current]||[]).concat(c[t.WILDCARD]||[])},a.isFinished=function(){return this.is(i)},a.error=e.error||function(t,e,n,r,i,a,o){throw o||a},a.states=function(){return Object.keys(c).sort()},r&&!r.defer&&a[r.event](),a},doCallback:function(e,n,r,i,a,o){if(n)try{return n.apply(e,[r,i,a].concat(o))}catch(n){return e.error(r,i,a,o,t.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",n)}},beforeAnyEvent:function(e,n,r,i,a){return t.doCallback(e,e.onbeforeevent,n,r,i,a)},afterAnyEvent:function(e,n,r,i,a){return t.doCallback(e,e.onafterevent||e.onevent,n,r,i,a)},leaveAnyState:function(e,n,r,i,a){return t.doCallback(e,e.onleavestate,n,r,i,a)},enterAnyState:function(e,n,r,i,a){return t.doCallback(e,e.onenterstate||e.onstate,n,r,i,a)},changeState:function(e,n,r,i,a){return t.doCallback(e,e.onchangestate,n,r,i,a)},beforeThisEvent:function(e,n,r,i,a){return t.doCallback(e,e["onbefore"+n],n,r,i,a)},afterThisEvent:function(e,n,r,i,a){return t.doCallback(e,e["onafter"+n]||e["on"+n],n,r,i,a)},leaveThisState:function(e,n,r,i,a){return t.doCallback(e,e["onleave"+r],n,r,i,a)},enterThisState:function(e,n,r,i,a){return t.doCallback(e,e["onenter"+i]||e["on"+i],n,r,i,a)},beforeEvent:function(e,n,r,i,a){if(!1===t.beforeThisEvent(e,n,r,i,a)||!1===t.beforeAnyEvent(e,n,r,i,a))return!1},afterEvent:function(e,n,r,i,a){t.afterThisEvent(e,n,r,i,a),t.afterAnyEvent(e,n,r,i,a)},leaveState:function(e,n,r,i,a){var o=t.leaveThisState(e,n,r,i,a),u=t.leaveAnyState(e,n,r,i,a);return!1!==o&&!1!==u&&(t.ASYNC===o||t.ASYNC===u?t.ASYNC:void 0)},enterState:function(e,n,r,i,a){t.enterThisState(e,n,r,i,a),t.enterAnyState(e,n,r,i,a)},buildEvent:function(e,n){return function(){var r=this.current,i=n[r]||(n[t.WILDCARD]!=t.WILDCARD?n[t.WILDCARD]:r)||r,a=Array.prototype.slice.call(arguments);if(this.transition)return this.error(e,r,i,a,t.Error.PENDING_TRANSITION,"event "+e+" inappropriate because previous transition did not complete");if(this.cannot(e))return this.error(e,r,i,a,t.Error.INVALID_TRANSITION,"event "+e+" inappropriate in current state "+this.current);if(!1===t.beforeEvent(this,e,r,i,a))return t.Result.CANCELLED;if(r===i)return t.afterEvent(this,e,r,i,a),t.Result.NOTRANSITION;var o=this;this.transition=function(){return o.transition=null,o.current=i,t.enterState(o,e,r,i,a),t.changeState(o,e,r,i,a),t.afterEvent(o,e,r,i,a),t.Result.SUCCEEDED},this.transition.cancel=function(){o.transition=null,t.afterEvent(o,e,r,i,a)};var u=t.leaveState(this,e,r,i,a);return!1===u?(this.transition=null,t.Result.CANCELLED):t.ASYNC===u?t.Result.PENDING:this.transition?this.transition():void 0}}};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=t),exports.StateMachine=t):"function"==typeof define&&define.amd?define(function(e){return t}):"undefined"!=typeof window?window.StateMachine=t:"undefined"!=typeof self&&(self.StateMachine=t)}();