UNPKG

30.1 kBJavaScriptView Raw
1!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).XStateInterpreter={})}(this,(function(t){"use strict";
2/*! *****************************************************************************
3 Copyright (c) Microsoft Corporation.
4
5 Permission to use, copy, modify, and/or distribute this software for any
6 purpose with or without fee is hereby granted.
7
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 PERFORMANCE OF THIS SOFTWARE.
15 ***************************************************************************** */var e,n,r=function(){return(r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function i(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return s}function s(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}!function(t){t.Start="xstate.start",t.Stop="xstate.stop",t.Raise="xstate.raise",t.Send="xstate.send",t.Cancel="xstate.cancel",t.NullEvent="",t.Assign="xstate.assign",t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.Log="xstate.log",t.Init="xstate.init",t.Invoke="xstate.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t.Update="xstate.update",t.Pure="xstate.pure",t.Choose="xstate.choose"}(e||(e={})),function(t){t.Parent="#_parent",t.Internal="#_internal"}(n||(n={}));var a,c={};function u(t,e){return"object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n&&"_event"in n?t.value:y(t)?f(t):"string"!=typeof t?t:f(function(t,e){try{return y(t)?t:t.toString().split(e)}catch(e){throw new Error("'".concat(t,"' is not a valid state path."))}}(t,e));var n}function f(t){if(1===t.length)return t[0];for(var e={},n=e,r=0;r<t.length-1;r++)r===t.length-2?n[t[r]]=t[r+1]:(n[t[r]]={},n=n[t[r]]);return e}function h(t){var e;return(e=[]).concat.apply(e,s([],o(t),!1))}function d(t){return void 0===t?[]:function(t){return y(t)?t:[t]}(t)}function l(t,e,n){var r,o;if(g(t))return t(e,n.data);var s={};try{for(var a=i(Object.keys(t)),c=a.next();!c.done;c=a.next()){var u=c.value,f=t[u];g(f)?s[u]=f(e,n.data):s[u]=f}}catch(t){r={error:t}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(r)throw r.error}}return s}function p(t){return t instanceof Promise||!(null===t||!g(t)&&"object"!=typeof t||!g(t.then))}function v(t,e,n,r){return t?n.reduce((function(t,n){var o,s,a=n.assignment,c={state:r,action:n,_event:e},u={};if(g(a))u=a(t,e.data,c);else try{for(var f=i(Object.keys(a)),h=f.next();!h.done;h=f.next()){var d=h.value,l=a[d];u[d]=g(l)?l(t,e.data,c):l}}catch(t){o={error:t}}finally{try{h&&!h.done&&(s=f.return)&&s.call(f)}finally{if(o)throw o.error}}return Object.assign({},t,u)}),t):t}function y(t){return Array.isArray(t)}function g(t){return"function"==typeof t}function m(t){return"string"==typeof t}var x=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function b(t){return!!t&&"__xstatenode"in t}(a={})[x]=function(){return this},a[Symbol.observable]=function(){return this};var w=function(){var t=0;return function(){return(++t).toString(16)}}();function S(t,e){return m(t)||"number"==typeof t?r({type:t},e):t}function _(t,e){if(!m(t)&&"$$type"in t&&"scxml"===t.$$type)return t;var n=S(t);return r({name:n.type,data:n,$$type:"scxml",type:"external"},e)}function E(t,e,n){var r=function(){},i="object"==typeof t,o=i?t:null;return{next:((i?t.next:t)||r).bind(o),error:((i?t.error:e)||r).bind(o),complete:((i?t.complete:n)||r).bind(o)}}var O=e.Start,I=e.Stop,T=e.Raise,A=e.Send,L=e.Cancel;e.NullEvent;var k=e.Assign;e.After,e.DoneState;var j=e.Log,C=e.Init;e.Invoke,e.ErrorExecution;var P=e.ErrorPlatform,N=e.ErrorCustom,D=e.Update,M=e.Choose,R=e.Pure,z=_({type:C});function V(t,e){return e&&e[t]||void 0}function J(t,e){var n;if(m(t)||"number"==typeof t)n=g(i=V(t,e))?{type:t,exec:i}:i||{type:t,exec:void 0};else if(g(t))n={type:t.name||t.toString(),exec:t};else{var i;if(g(i=V(t.type,e)))n=r(r({},t),{exec:i});else if(i){var o=i.type||t.type;n=r(r(r({},i),t),{type:o})}else n=t}return n}var q=function(t,e){return t?(y(t)?t:[t]).map((function(t){return J(t,e)})):[]};function U(t,n){var r="".concat(e.DoneInvoke,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}function $(t,n){var r="".concat(e.ErrorPlatform,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}function F(t,a,c,u,f,l,p){void 0===p&&(p=!1);var y=p?[]:function(t){var e,n,r=[];try{for(var o=i(t),s=o.next();!s.done;s=o.next())for(var a=s.value,c=0;c<a.length;)a[c].type!==k?c++:(r.push(a[c]),a.splice(c,1))}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}return r}(f),x=y.length?v(c,u,y,a):c,b=p?[c]:void 0,w=[];function S(i){var f;switch(i.type){case T:return{type:T,_event:_(i.event)};case A:var h=function(t,e,n,i){var o,s={_event:n},a=_(g(t.event)?t.event(e,n.data,s):t.event);if(m(t.delay)){var c=i&&i[t.delay];o=g(c)?c(e,n.data,s):c}else o=g(t.delay)?t.delay(e,n.data,s):t.delay;var u=g(t.to)?t.to(e,n.data,s):t.to;return r(r({},t),{to:u,_event:a,event:a.data,delay:o})}(i,x,u,t.options.delays);return l&&h.to!==n.Internal&&w.push(h),h;case j:var y=function(t,e,n){return r(r({},t),{value:m(t.expr)?t.expr:t.expr(e,n.data,{_event:n})})}(i,x,u);return null==l||l(y,x,u),y;case M:if(!(L=null===(f=i.conds.find((function(e){var n=function(t,e){if(t)return m(t)?{type:"xstate.guard",name:t,predicate:e?e[t]:void 0}:g(t)?{type:"xstate.guard",name:t.name,predicate:t}:t}(e.cond,t.options.guards);return!n||function(t,e,n,r,i){var o=t.options.guards,s={state:i,cond:e,_event:r};if("xstate.guard"===e.type)return((null==o?void 0:o[e.name])||e.predicate)(n,r.data,s);var a=null==o?void 0:o[e.type];if(!a)throw new Error("Guard '".concat(e.type,"' is not implemented on machine '").concat(t.id,"'."));return a(n,r.data,s)}(t,n,x,u,l?void 0:a)})))||void 0===f?void 0:f.actions))return[];var S=o(F(t,a,x,u,[q(d(L),t.options.actions)],l,p),2),E=S[0],O=S[1];return x=O,null==b||b.push(x),E;case R:var L;if(!(L=i.get(x,u.data)))return[];var C=o(F(t,a,x,u,[q(d(L),t.options.actions)],l,p),2),P=C[0],N=C[1];return x=N,null==b||b.push(x),P;case I:y=function(t,n,r){var i=g(t.activity)?t.activity(n,r.data):t.activity,o="string"==typeof i?{id:i}:i;return{type:e.Stop,activity:o}}(i,x,u);return null==l||l(y,c,u),y;case k:x=v(x,u,[i],l?void 0:a),null==b||b.push(x);break;default:var D=J(i,t.options.actions),z=D.exec;if(l)l(D,x,u);else if(z&&b){var V=b.length-1;D=r(r({},D),{exec:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];z.apply(void 0,s([b[V]],o(e),!1))}})}return D}}return[h(f.map((function(t){var e,n,r=[];try{for(var o=i(t),s=o.next();!s.done;s=o.next()){var a=S(s.value);a&&(r=r.concat(a))}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}return w.forEach((function(t){l(t,x,u)})),w.length=0,r}))),x]}var X=function(){function t(t){var e,n,r=this;this.actions=[],this.activities=c,this.meta={},this.events=[],this.value=t.value,this.context=t.context,this._event=t._event,this._sessionid=t._sessionid,this.event=this._event.data,this.historyValue=t.historyValue,this.history=t.history,this.actions=t.actions||[],this.activities=t.activities||c,this.meta=(void 0===(n=t.configuration)&&(n=[]),n.reduce((function(t,e){return void 0!==e.meta&&(t[e.id]=e.meta),t}),{})),this.events=t.events||[],this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration,this.transitions=t.transitions,this.children=t.children,this.done=!!t.done,this.tags=null!==(e=Array.isArray(t.tags)?new Set(t.tags):t.tags)&&void 0!==e?e:new Set,this.machine=t.machine,Object.defineProperty(this,"nextEvents",{get:function(){return function(t){return s([],o(new Set(h(s([],o(t.map((function(t){return t.ownEvents}))),!1)))),!1)}(r.configuration)}})}return t.from=function(e,n){return e instanceof t?e.context!==n?new t({value:e.value,context:n,_event:e._event,_sessionid:null,historyValue:e.historyValue,history:e.history,actions:[],activities:e.activities,meta:{},events:[],configuration:[],transitions:[],children:{}}):e:new t({value:e,context:n,_event:z,_sessionid:null,historyValue:void 0,history:void 0,actions:[],activities:void 0,meta:void 0,events:[],configuration:[],transitions:[],children:{}})},t.create=function(e){return new t(e)},t.inert=function(e,n){if(e instanceof t){if(!e.actions.length)return e;var r=z;return new t({value:e.value,context:n,_event:r,_sessionid:null,historyValue:e.historyValue,history:e.history,activities:e.activities,configuration:e.configuration,transitions:[],children:{}})}return t.from(e,n)},t.prototype.toStrings=function(t,e){var n=this;if(void 0===t&&(t=this.value),void 0===e&&(e="."),m(t))return[t];var r=Object.keys(t);return r.concat.apply(r,s([],o(r.map((function(r){return n.toStrings(t[r],e).map((function(t){return r+e+t}))}))),!1))},t.prototype.toJSON=function(){var t=this;t.configuration,t.transitions;var e=t.tags;t.machine;var n=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}(t,["configuration","transitions","tags","machine"]);return r(r({},n),{tags:Array.from(e)})},t.prototype.matches=function(t){return function t(e,n,r){void 0===r&&(r=".");var i=u(e,r),o=u(n,r);return m(o)?!!m(i)&&o===i:m(i)?i in o:Object.keys(i).every((function(e){return e in o&&t(i[e],o[e])}))}(t,this.value)},t.prototype.hasTag=function(t){return this.tags.has(t)},t.prototype.can=function(t){var e;this.machine;var n=null===(e=this.machine)||void 0===e?void 0:e.getTransitionData(this,t);return!!(null==n?void 0:n.transitions.length)&&n.transitions.some((function(t){return void 0!==t.target||t.actions.length}))},t}(),Q={deferEvents:!1},B=function(){function t(t){this.processingEvent=!1,this.queue=[],this.initialized=!1,this.options=r(r({},Q),t)}return t.prototype.initialize=function(t){if(this.initialized=!0,t){if(!this.options.deferEvents)return void this.schedule(t);this.process(t)}this.flushEvents()},t.prototype.schedule=function(t){if(this.initialized&&!this.processingEvent){if(0!==this.queue.length)throw new Error("Event queue should be empty when it is not processing events");this.process(t),this.flushEvents()}else this.queue.push(t)},t.prototype.clear=function(){this.queue=[]},t.prototype.flushEvents=function(){for(var t=this.queue.shift();t;)this.process(t),t=this.queue.shift()},t.prototype.process=function(t){this.processingEvent=!0;try{t()}catch(t){throw this.clear(),t}finally{this.processingEvent=!1}},t}(),G=[],H=function(t,e){G.push(t);var n=e(t);return G.pop(),n};function K(t,e,n){var r=function(t){var e;return(e={id:t,send:function(){},subscribe:function(){return{unsubscribe:function(){}}},getSnapshot:function(){},toJSON:function(){return{id:t}}})[x]=function(){return this},e}(e);if(r.deferred=!0,b(t)){var i=r.state=H(void 0,(function(){return(n?t.withContext(n):t).initialState}));r.getSnapshot=function(){return i}}return r}var W=new Map,Y=0,Z=function(){return"x:".concat(Y++)},tt=function(t,e){return W.set(t,e),t},et=function(t){return W.get(t)},nt=function(t){W.delete(t)};function rt(){return"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}function it(t){if(rt()){var e=function(){var t=rt();if(t&&"__xstate__"in t)return t.__xstate__}();e&&e.register(t)}}function ot(t,e){void 0===e&&(e={});var n=t.initialState,i=new Set,o=[],s=!1,a=function(t){var e;return r(((e={subscribe:function(){return{unsubscribe:function(){}}},id:"anonymous",getSnapshot:function(){}})[x]=function(){return this},e),t)}({id:e.id,send:function(e){o.push(e),function(){if(!s){for(s=!0;o.length>0;){var e=o.shift();n=t.transition(n,e,c),i.forEach((function(t){return t.next(n)}))}s=!1}}()},getSnapshot:function(){return n},subscribe:function(t,e,r){var o=E(t,e,r);return i.add(o),o.next(n),{unsubscribe:function(){i.delete(o)}}}}),c={parent:e.parent,self:a,id:e.id||"anonymous",observers:i};return n=t.start?t.start(c):n,a}var st,at={sync:!1,autoForward:!1};t.InterpreterStatus=void 0,(st=t.InterpreterStatus||(t.InterpreterStatus={}))[st.NotStarted=0]="NotStarted",st[st.Running=1]="Running",st[st.Stopped=2]="Stopped";var ct=function(){function a(i,o){void 0===o&&(o=a.defaultOptions);var s=this;this.machine=i,this.delayedEventsMap={},this.listeners=new Set,this.contextListeners=new Set,this.stopListeners=new Set,this.doneListeners=new Set,this.eventListeners=new Set,this.sendListeners=new Set,this.initialized=!1,this.status=t.InterpreterStatus.NotStarted,this.children=new Map,this.forwardTo=new Set,this._outgoingQueue=[],this.init=this.start,this.send=function(e,n){if(y(e))return s.batch(e),s.state;var r=_(S(e,n));if(s.status===t.InterpreterStatus.Stopped)return s.state;if(s.status!==t.InterpreterStatus.Running&&!s.options.deferEvents)throw new Error('Event "'.concat(r.name,'" was sent to uninitialized service "').concat(s.machine.id,'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: ').concat(JSON.stringify(r.data)));return s.scheduler.schedule((function(){s.forward(r);var t=s._nextState(r);s.update(t,r)})),s._state},this.sendTo=function(e,i,o){var a,c=s.parent&&(i===n.Parent||s.parent.id===i),u=c?s.parent:m(i)?s.children.get(i)||et(i):(a=i)&&"function"==typeof a.send?i:void 0;if(u)if("machine"in u){if(s.status!==t.InterpreterStatus.Stopped||s.parent!==u||s.state.done){var f=r(r({},e),{name:e.name===N?"".concat($(s.id)):e.name,origin:s.sessionId});!o&&s.machine.config.predictableActionArguments?s._outgoingQueue.push([u,f]):u.send(f)}}else!o&&s.machine.config.predictableActionArguments?s._outgoingQueue.push([u,e.data]):u.send(e.data);else if(!c)throw new Error("Unable to send event to child '".concat(i,"' from service '").concat(s.id,"'."))},this._exec=function(n,r,i,o){void 0===o&&(o=s.machine.options.actions);var a,c=n.exec||V(n.type,o),u=g(c)?c:c?c.exec:n.exec;if(u)try{return u(r,i.data,s.machine.config.predictableActionArguments?{action:n,_event:i}:{action:n,state:s.state,_event:i})}catch(t){throw s.parent&&s.parent.send({type:"xstate.error",data:t}),t}switch(n.type){case A:var f=n;if("number"==typeof f.delay)return void s.defer(f);f.to?s.sendTo(f._event,f.to,i===z):s.send(f._event);break;case L:s.cancel(n.sendId);break;case O:if(s.status!==t.InterpreterStatus.Running)return;var h=n.activity;if(!s.machine.config.predictableActionArguments&&!s.state.activities[h.id||h.type])break;if(h.type===e.Invoke){var d="string"==typeof(a=h.src)?{type:a}:a,p=s.machine.options.services?s.machine.options.services[d.type]:void 0,v=h.id,y=h.data,m="autoForward"in h?h.autoForward:!!h.forward;if(!p)return;var x=y?l(y,r,i):void 0;if("string"==typeof p)return;var w=g(p)?p(r,i.data,{data:x,src:d,meta:h.meta}):p;if(!w)return;var S=void 0;b(w)&&(w=x?w.withContext(x):w,S={autoForward:m}),s.spawn(w,v,S)}else s.spawnActivity(h);break;case I:s.stopChild(n.activity.id);break;case j:var _=n.label,E=n.value;_?s.logger(_,E):s.logger(E)}};var c=r(r({},a.defaultOptions),o),u=c.clock,f=c.logger,h=c.parent,d=c.id,p=void 0!==d?d:i.id;this.id=p,this.logger=f,this.clock=u,this.parent=h,this.options=c,this.scheduler=new B({deferEvents:this.options.deferEvents}),this.sessionId=Z()}return Object.defineProperty(a.prototype,"initialState",{get:function(){var t=this;return this._initialState?this._initialState:H(this,(function(){return t._initialState=t.machine.initialState,t._initialState}))},enumerable:!1,configurable:!0}),Object.defineProperty(a.prototype,"state",{get:function(){return this._state},enumerable:!1,configurable:!0}),a.prototype.execute=function(t,e){var n,r;try{for(var o=i(t.actions),s=o.next();!s.done;s=o.next()){var a=s.value;this.exec(a,t,e)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}},a.prototype.update=function(t,e){var n,r,o,s,a,c,u,f,h=this;if(t._sessionid=this.sessionId,this._state=t,this.machine.config.predictableActionArguments&&e!==z||!this.options.execute)for(var d=void 0;d=this._outgoingQueue.shift();)d[0].send(d[1]);else this.execute(this.state);if(this.children.forEach((function(t){h.state.children[t.id]=t})),this.devTools&&this.devTools.send(e.data,t),t.event)try{for(var p=i(this.eventListeners),v=p.next();!v.done;v=p.next()){(0,v.value)(t.event)}}catch(t){n={error:t}}finally{try{v&&!v.done&&(r=p.return)&&r.call(p)}finally{if(n)throw n.error}}try{for(var y=i(this.listeners),g=y.next();!g.done;g=y.next()){(0,g.value)(t,t.event)}}catch(t){o={error:t}}finally{try{g&&!g.done&&(s=y.return)&&s.call(y)}finally{if(o)throw o.error}}try{for(var m=i(this.contextListeners),x=m.next();!x.done;x=m.next()){(0,x.value)(this.state.context,this.state.history?this.state.history.context:void 0)}}catch(t){a={error:t}}finally{try{x&&!x.done&&(c=m.return)&&c.call(m)}finally{if(a)throw a.error}}if(this.state.done){var b=t.configuration.find((function(t){return"final"===t.type&&t.parent===h.machine})),w=b&&b.doneData?l(b.doneData,t.context,e):void 0;try{for(var S=i(this.doneListeners),_=S.next();!_.done;_=S.next()){(0,_.value)(U(this.id,w))}}catch(t){u={error:t}}finally{try{_&&!_.done&&(f=S.return)&&f.call(S)}finally{if(u)throw u.error}}this._stop(),this._stopChildren()}},a.prototype.onTransition=function(e){return this.listeners.add(e),this.status===t.InterpreterStatus.Running&&e(this.state,this.state.event),this},a.prototype.subscribe=function(e,n,r){var i=this,o=E(e,n,r);this.listeners.add(o.next),this.status!==t.InterpreterStatus.NotStarted&&o.next(this.state);var s=function(){i.doneListeners.delete(s),i.stopListeners.delete(s),o.complete()};return this.status===t.InterpreterStatus.Stopped?o.complete():(this.onDone(s),this.onStop(s)),{unsubscribe:function(){i.listeners.delete(o.next),i.doneListeners.delete(s),i.stopListeners.delete(s)}}},a.prototype.onEvent=function(t){return this.eventListeners.add(t),this},a.prototype.onSend=function(t){return this.sendListeners.add(t),this},a.prototype.onChange=function(t){return this.contextListeners.add(t),this},a.prototype.onStop=function(t){return this.stopListeners.add(t),this},a.prototype.onDone=function(t){return this.doneListeners.add(t),this},a.prototype.off=function(t){return this.listeners.delete(t),this.eventListeners.delete(t),this.sendListeners.delete(t),this.stopListeners.delete(t),this.doneListeners.delete(t),this.contextListeners.delete(t),this},a.prototype.start=function(e){var n=this;if(this.status===t.InterpreterStatus.Running)return this;this.machine._init(),tt(this.sessionId,this),this.initialized=!0,this.status=t.InterpreterStatus.Running;var r=void 0===e?this.initialState:H(this,(function(){return"object"==typeof(t=e)&&null!==t&&"value"in t&&"_event"in t?n.machine.resolveState(e):n.machine.resolveState(X.from(e,n.machine.context));var t}));return this.options.devTools&&this.attachDev(),this.scheduler.initialize((function(){n.update(r,z)})),this},a.prototype._stopChildren=function(){this.children.forEach((function(t){g(t.stop)&&t.stop()})),this.children.clear()},a.prototype._stop=function(){var e,n,r,o,s,a,c,u,f,h;try{for(var d=i(this.listeners),l=d.next();!l.done;l=d.next()){var p=l.value;this.listeners.delete(p)}}catch(t){e={error:t}}finally{try{l&&!l.done&&(n=d.return)&&n.call(d)}finally{if(e)throw e.error}}try{for(var v=i(this.stopListeners),y=v.next();!y.done;y=v.next()){(p=y.value)(),this.stopListeners.delete(p)}}catch(t){r={error:t}}finally{try{y&&!y.done&&(o=v.return)&&o.call(v)}finally{if(r)throw r.error}}try{for(var g=i(this.contextListeners),m=g.next();!m.done;m=g.next()){p=m.value;this.contextListeners.delete(p)}}catch(t){s={error:t}}finally{try{m&&!m.done&&(a=g.return)&&a.call(g)}finally{if(s)throw s.error}}try{for(var x=i(this.doneListeners),b=x.next();!b.done;b=x.next()){p=b.value;this.doneListeners.delete(p)}}catch(t){c={error:t}}finally{try{b&&!b.done&&(u=x.return)&&u.call(x)}finally{if(c)throw c.error}}if(!this.initialized)return this;this.initialized=!1,this.status=t.InterpreterStatus.Stopped,this._initialState=void 0;try{for(var w=i(Object.keys(this.delayedEventsMap)),S=w.next();!S.done;S=w.next()){var _=S.value;this.clock.clearTimeout(this.delayedEventsMap[_])}}catch(t){f={error:t}}finally{try{S&&!S.done&&(h=w.return)&&h.call(w)}finally{if(f)throw f.error}}this.scheduler.clear(),this.scheduler=new B({deferEvents:this.options.deferEvents})},a.prototype.stop=function(){var t=this,e=this.scheduler;return this._stop(),e.schedule((function(){var e=_({type:"xstate.stop"}),r=H(t,(function(){var r=h(s([],o(t.state.configuration),!1).sort((function(t,e){return e.order-t.order})).map((function(e){return q(e.onExit,t.machine.options.actions)}))),i=o(F(t.machine,t.state,t.state.context,e,[r],t.machine.config.predictableActionArguments?t._exec:void 0,t.machine.config.predictableActionArguments||t.machine.config.preserveActionOrder),2),a=i[0],c=i[1],u=new X({value:t.state.value,context:c,_event:e,_sessionid:t.sessionId,historyValue:void 0,history:t.state,actions:a.filter((function(t){return t.type!==T&&(t.type!==A||!!t.to&&t.to!==n.Internal)})),activities:{},events:[],configuration:[],transitions:[],children:{},done:t.state.done,tags:t.state.tags,machine:t.machine});return u.changed=!0,u}));t.update(r,e),t._stopChildren(),nt(t.sessionId)})),this},a.prototype.batch=function(e){var n=this;if(this.status===t.InterpreterStatus.NotStarted&&this.options.deferEvents);else if(this.status!==t.InterpreterStatus.Running)throw new Error("".concat(e.length,' event(s) were sent to uninitialized service "').concat(this.machine.id,'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.'));if(e.length){var a=!!this.machine.config.predictableActionArguments&&this._exec;this.scheduler.schedule((function(){var t,c,u=n.state,f=!1,h=[],d=function(t){var e=_(t);n.forward(e),u=H(n,(function(){return n.machine.transition(u,e,void 0,a||void 0)})),h.push.apply(h,s([],o(n.machine.config.predictableActionArguments?u.actions:u.actions.map((function(t){return function(t,e){var n=t.exec;return r(r({},t),{exec:void 0!==n?function(){return n(e.context,e.event,{action:t,state:e,_event:e._event})}:void 0})}(t,u)}))),!1)),f=f||!!u.changed};try{for(var l=i(e),p=l.next();!p.done;p=l.next()){d(p.value)}}catch(e){t={error:e}}finally{try{p&&!p.done&&(c=l.return)&&c.call(l)}finally{if(t)throw t.error}}u.changed=f,u.actions=h,n.update(u,_(e[e.length-1]))}))}},a.prototype.sender=function(t){return this.send.bind(this,t)},a.prototype._nextState=function(t,e){var n=this;void 0===e&&(e=!!this.machine.config.predictableActionArguments&&this._exec);var r=_(t);if(0===r.name.indexOf(P)&&!this.state.nextEvents.some((function(t){return 0===t.indexOf(P)})))throw r.data.data;return H(this,(function(){return n.machine.transition(n.state,r,void 0,e||void 0)}))},a.prototype.nextState=function(t){return this._nextState(t,!1)},a.prototype.forward=function(t){var e,n;try{for(var r=i(this.forwardTo),o=r.next();!o.done;o=r.next()){var s=o.value,a=this.children.get(s);if(!a)throw new Error("Unable to forward event '".concat(t,"' from interpreter '").concat(this.id,"' to nonexistant child '").concat(s,"'."));a.send(t)}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(e)throw e.error}}},a.prototype.defer=function(t){var e=this;this.delayedEventsMap[t.id]=this.clock.setTimeout((function(){t.to?e.sendTo(t._event,t.to,!0):e.send(t._event)}),t.delay)},a.prototype.cancel=function(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]},a.prototype.exec=function(t,e,n){void 0===n&&(n=this.machine.options.actions),this._exec(t,e.context,e._event,n)},a.prototype.removeChild=function(t){var e;this.children.delete(t),this.forwardTo.delete(t),null===(e=this.state)||void 0===e||delete e.children[t]},a.prototype.stopChild=function(t){var e=this.children.get(t);e&&(this.removeChild(t),g(e.stop)&&e.stop())},a.prototype.spawn=function(e,n,i){if(this.status!==t.InterpreterStatus.Running)return K(e,n);if(p(e))return this.spawnPromise(Promise.resolve(e),n);if(g(e))return this.spawnCallback(e,n);if(function(t){try{return"function"==typeof t.send}catch(t){return!1}}(s=e)&&"id"in s)return this.spawnActor(e,n);if(function(t){try{return"subscribe"in t&&g(t.subscribe)}catch(t){return!1}}(e))return this.spawnObservable(e,n);if(b(e))return this.spawnMachine(e,r(r({},i),{id:n}));if(null!==(o=e)&&"object"==typeof o&&"transition"in o&&"function"==typeof o.transition)return this.spawnBehavior(e,n);throw new Error('Unable to spawn entity "'.concat(n,'" of type "').concat(typeof e,'".'));var o,s},a.prototype.spawnMachine=function(t,e){var n=this;void 0===e&&(e={});var i=new a(t,r(r({},this.options),{parent:this,id:e.id||t.id})),o=r(r({},at),e);o.sync&&i.onTransition((function(t){n.send(D,{state:t,id:i.id})}));var s=i;return this.children.set(i.id,s),o.autoForward&&this.forwardTo.add(i.id),i.onDone((function(t){n.removeChild(i.id),n.send(_(t,{origin:i.id}))})).start(),s},a.prototype.spawnBehavior=function(t,e){var n=ot(t,{id:e,parent:this});return this.children.set(e,n),n},a.prototype.spawnPromise=function(t,e){var n,r,i=this,o=!1;t.then((function(t){o||(r=t,i.removeChild(e),i.send(_(U(e,t),{origin:e})))}),(function(t){if(!o){i.removeChild(e);var n=$(e,t);try{i.send(_(n,{origin:e}))}catch(t){i.devTools&&i.devTools.send(n,i.state),i.machine.strict&&i.stop()}}}));var s=((n={id:e,send:function(){},subscribe:function(e,n,r){var i=E(e,n,r),o=!1;return t.then((function(t){o||(i.next(t),o||i.complete())}),(function(t){o||i.error(t)})),{unsubscribe:function(){return o=!0}}},stop:function(){o=!0},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[x]=function(){return this},n);return this.children.set(e,s),s},a.prototype.spawnCallback=function(t,e){var n,r,i,o=this,s=!1,a=new Set,c=new Set;try{i=t((function(t){r=t,c.forEach((function(e){return e(t)})),s||o.send(_(t,{origin:e}))}),(function(t){a.add(t)}))}catch(t){this.send($(e,t))}if(p(i))return this.spawnPromise(i,e);var u=((n={id:e,send:function(t){return a.forEach((function(e){return e(t)}))},subscribe:function(t){var e=E(t);return c.add(e.next),{unsubscribe:function(){c.delete(e.next)}}},stop:function(){s=!0,g(i)&&i()},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[x]=function(){return this},n);return this.children.set(e,u),u},a.prototype.spawnObservable=function(t,e){var n,r,i=this,o=t.subscribe((function(t){r=t,i.send(_(t,{origin:e}))}),(function(t){i.removeChild(e),i.send(_($(e,t),{origin:e}))}),(function(){i.removeChild(e),i.send(_(U(e),{origin:e}))})),s=((n={id:e,send:function(){},subscribe:function(e,n,r){return t.subscribe(e,n,r)},stop:function(){return o.unsubscribe()},getSnapshot:function(){return r},toJSON:function(){return{id:e}}})[x]=function(){return this},n);return this.children.set(e,s),s},a.prototype.spawnActor=function(t,e){return this.children.set(e,t),t},a.prototype.spawnActivity=function(t){var e=this.machine.options&&this.machine.options.activities?this.machine.options.activities[t.type]:void 0;if(e){var n=e(this.state.context,t);this.spawnEffect(t.id,n)}},a.prototype.spawnEffect=function(t,e){var n;this.children.set(t,((n={id:t,send:function(){},subscribe:function(){return{unsubscribe:function(){}}},stop:e||void 0,getSnapshot:function(){},toJSON:function(){return{id:t}}})[x]=function(){return this},n))},a.prototype.attachDev=function(){var t=rt();if(this.options.devTools&&t){if(t.__REDUX_DEVTOOLS_EXTENSION__){var e="object"==typeof this.options.devTools?this.options.devTools:void 0;this.devTools=t.__REDUX_DEVTOOLS_EXTENSION__.connect(r(r({name:this.id,autoPause:!0,stateSanitizer:function(t){return{value:t.value,context:t.context,actions:t.actions}}},e),{features:r({jump:!1,skip:!1},e?e.features:void 0)}),this.machine),this.devTools.init(this.state)}it(this)}},a.prototype.toJSON=function(){return{id:this.id}},a.prototype[x]=function(){return this},a.prototype.getSnapshot=function(){return this.status===t.InterpreterStatus.NotStarted?this.initialState:this._state},a.defaultOptions={execute:!0,deferEvents:!0,clock:{setTimeout:function(t,e){return setTimeout(t,e)},clearTimeout:function(t){return clearTimeout(t)}},logger:console.log.bind(console),devTools:!1},a.interpret=ut,a}();function ut(t,e){return new ct(t,e)}t.Interpreter=ct,t.interpret=ut,t.spawn=function(t,e){var n=function(t){return m(t)?r(r({},at),{name:t}):r(r(r({},at),{name:w()}),t)}(e);return function(e){return e?e.spawn(t,n.name,n):K(t,n.name)}(G[G.length-1])},Object.defineProperty(t,"__esModule",{value:!0})}));