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