redux-automata
Version:
Finite state automata for Redux.
2 lines (1 loc) • 6.38 kB
JavaScript
var t="@@AUTOMATA",n=function(){function t(t,n){this.options=t,this.stateOptions=n}var n=t.prototype;return n.in=function(t){return this.options.in(t)},n.on=function(t){return this.stateOptions.on(t)},t}(),e=function(){function t(t,n,e,r){this.sourceStates=t,this.actionType=n,this.smOptions=e,this.stateOptions=r,this.transitions=[]}var e=t.prototype;return e.goTo=function(t){return this.targetState=t.stateName,new n(this.smOptions,this.stateOptions)},e.execute=function(t){return this.transitions.push(t),this},e.noop=function(){return new n(this.smOptions,this.stateOptions)},e.createArcs=function(){var t=this.actionType,n=this.targetState,e=this.transitions;return this.sourceStates.map((function(r){return{actionType:t,sourceState:r,targetState:n,transitions:e}}))},t}(),r=function(){function t(t,n){this.sourceStates=t,this.smOptions=n,this.builders=[]}var n=t.prototype;return n.on=function(t){var n=new e(this.sourceStates,t.actionType,this.smOptions,this);return this.builders.push(n),n},n.or=function(t){return this.sourceStates.push(t.stateName),this},n.getArcs=function(){return this.builders.reduce((function(t,n){return t.concat(n.createArcs())}),[])},t}(),i=function(){function t(t){this.automataName=t,this.states=[],this.options=[],this.initial=Object.assign({}),this.current=this.initial}var n=t.prototype;return n.in=function(t){var n=this.states.find((function(n){return n.stateName===t.stateName}));if(!n)throw Error("State should be defined using this.state(...) method.");var e=new r([n.stateName],this);return this.options.push(e),e},n.inAny=function(){var t=new r(this.states.map((function(t){return t.stateName})),this);return this.options.push(t),t},n.beginWith=function(t){this.graphCache=void 0;var n=this.states.find((function(n){return n.stateName===t.stateName}));if(!n)throw Error("State should be previously defined using this.state(...) method.");this.initial=Object.assign(t({},void 0),{__sm_state:n.stateName})},n.state=function(t,n){if(this.graphCache=void 0,!t)throw Error("State name can't be empty, null or undefined.");if(this.states.find((function(n){return n.stateName===t})))throw Error("State with the same name already exist: "+t);var e=Object.assign(n,{stateName:t});return this.states.push(e),e},n.action=function(t){var n=this;this.graphCache=void 0;var e="@@AUTOMATA "+this.automataName+" / "+t;return Object.assign((function(t){return{payload:t,type:e}}),{actionType:e,isInvocable:function(t){return n.hasTransition(t.__sm_state||"",e)}})},n.getGraph=function(){if(this.graphCache)return this.graphCache;var t=this.options.reduce((function(t,n){return t.concat(n.getArcs())}),[]);return this.graphCache=this.states.map((function(n){var e=t.filter((function(t){return t.sourceState===n.stateName})).map((function(t){return{actionType:t.actionType,targetState:t.targetState,transitions:t.transitions}}));return{actions:e=e.filter((function(t,n){return e.findIndex((function(n){return n.actionType===t.actionType}))===n})),entry:n}})),this.graphCache},n.hasTransition=function(t,n){var e=this.getGraph().find((function(n){return n.entry.stateName===t}));return void 0!==e&&e.actions.findIndex((function(t){return t.actionType===n}))>-1},n.mergeState=function(t){return Object.assign({},this.current,t)},t}();function s(t){return function(n){return function(e){if(!e.type.startsWith("@@AUTOMATA"))return n(e);var r=!0;Object.assign(e,{dispatch:function(n){return r?(setTimeout((function(){return t.dispatch(n)}),0),n):t.dispatch(n)}});var i=n(e);return r=!1,i}}}function a(t){if(void 0===t.initial.__sm_state)throw Error("No initial state specified. Use BeginWith() method to specify initial state.");t.current=t.initial;var n=t.getGraph();if(!n.find((function(n){return n.entry.stateName===t.current.__sm_state})))throw Error("Can't find initial state.");return function(e,r){if(void 0===e&&(e=t.initial),"string"!=typeof r.type||!r.type.startsWith("@@AUTOMATA"))return e;var i=n.find((function(t){return t.entry.stateName===e.__sm_state}));if(!i)return e;var s=i.actions.find((function(t){return t.actionType===r.type}));if(!s)return e;var a=e;if(s.targetState){var o=n.find((function(t){return t.entry.stateName===s.targetState}));if(!o)throw Error("Can't find state "+s.targetState);t.current=e,(a=o.entry(e,r.payload)).__sm_state=o.entry.stateName,t.current=a}if(!r.dispatch)throw Error("Dispatch is not defined to perform transitions. It seems `automataMiddleware` was not applied.");var u=Object.assign(r.dispatch,{dispatch:r.dispatch,getState:function(){return t.current}});return s.transitions.forEach((function(t){return t(u,r.payload)})),a}}function o(t,n){return(o=Object.setPrototypeOf||function(t,n){return t.__proto__=n,t})(t,n)}var u=function(t){var n,e;function r(n,e,r,i){var s;return(s=t.call(this,n+" Automata")||this).Name=n,s.Process=e,s.OnSuccess=r,s.OnFailure=i,s.Idle=s.state("Idle",(function(){return s.getDefaultState()})),s.Processing=s.state("Processing",(function(){return s.mergeState({error:null,isProcessing:!0})})),s.Completed=s.state("Completed",(function(t,n){return s.mergeState({error:null,isProcessing:!1,result:n})})),s.Failure=s.state("Failure",(function(t,n){return s.mergeState({error:n,isProcessing:!1})})),s.Start=s.action(s.Name+" Start"),s.Restart=s.action(s.Name+" Restart"),s.Cancel=s.action(s.Name+" Cancel"),s.End=s.action(s.Name+" Success"),s.Fail=s.action(s.Name+" Fail"),s.BeginProcessing=function(t,n){return s.Process(n).then((function(e){var r=t(s.End(e));return s.OnSuccess&&s.OnSuccess(t,e,n),r})).catch((function(e){var r=t(s.Fail(e));return s.OnFailure&&s.OnFailure(t,e,n),r}))},s}(n=r).prototype=Object.create((e=t).prototype),n.prototype.constructor=n,o(n,e);var i=r.prototype;return i.setupProcessIn=function(t){var n=this.Idle,e=this.Processing,r=this.Completed,i=this.Failure,s=this.Start,a=this.Restart,o=this.End,u=this.Fail,c=this.Cancel,h=this.BeginProcessing;this.in(t).on(s).execute(h).goTo(e).in(e).on(o).goTo(r).on(u).goTo(i).on(c).goTo(n).in(i).on(c).goTo(n).in(r).or(i).on(a).execute(h).goTo(e)},i.getDefaultState=function(){return{isProcessing:!1}},r}(i);function c(t,n,e,r){var i=new u(t,n,e,r);i.setupProcessIn(i.Idle),i.beginWith(i.Idle);var s=a(i);return{cancel:i.Cancel,reducer:s,restart:i.Restart,start:i.Start}}export{t as ACTION_TYPE_PREFIX,i as Automata,u as TaskAutomata,s as automataMiddleware,a as automataReducer,c as createTaskAutomation};