UNPKG

30.8 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=e.Start,u=e.Stop,f=e.Raise,h=e.Send,d=e.Cancel;e.NullEvent;var l=e.Assign;e.After,e.DoneState;var p=e.Log,v=e.Init;e.Invoke,e.ErrorExecution;var y,g=e.ErrorPlatform,m=e.ErrorCustom,x=e.Update,b=e.Choose,S=e.Pure;function w(t,e){return"object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n&&"_event"in n?t.value:L(t)?_(t):"string"!=typeof t?t:_(function(t,e){try{return L(t)?t:t.toString().split(e)}catch(e){throw new Error("'".concat(t,"' is not a valid state path."))}}(t,e));var n}function _(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 E(t){var e;return(e=[]).concat.apply(e,s([],o(t),!1))}function O(t){return void 0===t?[]:function(t){return L(t)?t:[t]}(t)}function I(t,e,n){var r,o;if(k(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];k(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 T(t){return t instanceof Promise||!(null===t||!k(t)&&"object"!=typeof t||!k(t.then))}function A(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(k(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]=k(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 L(t){return Array.isArray(t)}function k(t){return"function"==typeof t}function j(t){return"string"==typeof t}var C=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function P(t){return!!t&&"__xstatenode"in t}(y={})[C]=function(){return this},y[Symbol.observable]=function(){return this};var N=function(){var t=0;return function(){return(++t).toString(16)}}();function D(t,e){return j(t)||"number"==typeof t?r({type:t},e):t}function M(t,e){if(!j(t)&&"$$type"in t&&"scxml"===t.$$type)return t;var n=D(t);return r({name:n.type,data:n,$$type:"scxml",type:"external"},e)}function R(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 z=M({type:v});function V(t,e){return e&&e[t]||void 0}function J(t,e){var n;if(j(t)||"number"==typeof t)n=k(i=V(t,e))?{type:t,exec:i}:i||{type:t,exec:void 0};else if(k(t))n={type:t.name||t.toString(),exec:t};else{var i;if(k(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?(L(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,d,v,y,g){void 0===g&&(g=!1);var m=g?[]: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.actions.length;)a.actions[c].type!==l?c++:(r.push(a.actions[c]),a.actions.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}(v),x=m.length?A(c,d,m,a):c,w=g?[c]:void 0,_=[];function I(i,v){var m;switch(v.type){case f:var E=function(t,e,n,i){var o,s={_event:n},a=M(k(t.event)?t.event(e,n.data,s):t.event);if(j(t.delay)){var c=i&&i[t.delay];o=k(c)?c(e,n.data,s):c}else o=k(t.delay)?t.delay(e,n.data,s):t.delay;return r(r({},t),{type:f,_event:a,delay:o})}(v,x,d,t.options.delays);return y&&"number"==typeof E.delay&&y(E,x,d),E;case h:var I=function(t,e,n,i){var o,s={_event:n},a=M(k(t.event)?t.event(e,n.data,s):t.event);if(j(t.delay)){var c=i&&i[t.delay];o=k(c)?c(e,n.data,s):c}else o=k(t.delay)?t.delay(e,n.data,s):t.delay;var u=k(t.to)?t.to(e,n.data,s):t.to;return r(r({},t),{to:u,_event:a,event:a.data,delay:o})}(v,x,d,t.options.delays);return y&&I.to!==n.Internal&&("entry"===i?_.push(I):y(I,x,d)),I;case p:var T=function(t,e,n){return r(r({},t),{value:j(t.expr)?t.expr:t.expr(e,n.data,{_event:n})})}(v,x,d);return null==y||y(T,x,d),T;case b:if(!(N=null===(m=v.conds.find((function(e){var n=function(t,e){if(t)return j(t)?{type:"xstate.guard",name:t,predicate:e?e[t]:void 0}:k(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,d,y?void 0:a)})))||void 0===m?void 0:m.actions))return[];var L=o(F(t,a,x,d,[{type:i,actions:q(O(N),t.options.actions)}],y,g),2),C=L[0],P=L[1];return x=P,null==w||w.push(x),C;case S:var N;if(!(N=v.get(x,d.data)))return[];var D=o(F(t,a,x,d,[{type:i,actions:q(O(N),t.options.actions)}],y,g),2),R=D[0],z=D[1];return x=z,null==w||w.push(x),R;case u:T=function(t,n,r){var i=k(t.activity)?t.activity(n,r.data):t.activity,o="string"==typeof i?{id:i}:i;return{type:e.Stop,activity:o}}(v,x,d);return null==y||y(T,c,d),T;case l:x=A(x,d,[v],y?void 0:a),null==w||w.push(x);break;default:var V=J(v,t.options.actions),U=V.exec;if(y)y(V,x,d);else if(U&&w){var $=w.length-1;V=r(r({},V),{exec:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];U.apply(void 0,s([w[$]],o(e),!1))}})}return V}}return[E(v.map((function(t){var e,n,r=[];try{for(var o=i(t.actions),s=o.next();!s.done;s=o.next()){var a=s.value,c=I(t.type,a);c&&(r=r.concat(c))}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}return _.forEach((function(t){y(t,x,d)})),_.length=0,r}))),x]}var X=function(){function t(t){var e,n,r=this;this.actions=[],this.activities=a,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||a,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(E(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="."),j(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=w(e,r),o=w(n,r);return j(o)?!!j(i)&&o===i:j(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}}})[C]=function(){return this},e}(e);if(r.deferred=!0,P(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(){}})[C]=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=R(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(L(e))return s.batch(e),s.state;var r=M(D(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:j(i)?i===n.Internal?s: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===m?"".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,l=n.exec||V(n.type,o),v=k(l)?l:l?l.exec:n.exec;if(v)try{return v(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 f:var y=n;s.defer(y);break;case h:var g=n;if("number"==typeof g.delay)return void s.defer(g);g.to?s.sendTo(g._event,g.to,i===z):s.send(g._event);break;case d:s.cancel(n.sendId);break;case c:if(s.status!==t.InterpreterStatus.Running)return;var m=n.activity;if(!s.machine.config.predictableActionArguments&&!s.state.activities[m.id||m.type])break;if(m.type===e.Invoke){var x="string"==typeof(a=m.src)?{type:a}:a,b=s.machine.options.services?s.machine.options.services[x.type]:void 0,S=m.id,w=m.data,_="autoForward"in m?m.autoForward:!!m.forward;if(!b)return;var E=w?I(w,r,i):void 0;if("string"==typeof b)return;var O=k(b)?b(r,i.data,{data:E,src:x,meta:m.meta}):b;if(!O)return;var T=void 0;P(O)&&(O=E?O.withContext(E):O,T={autoForward:_}),s.spawn(O,S,T)}else s.spawnActivity(m);break;case u:s.stopChild(n.activity.id);break;case p:var A=n,L=A.label,j=A.value;L?s.logger(L,j):s.logger(j)}};var l=r(r({},a.defaultOptions),o),v=l.clock,y=l.logger,g=l.parent,x=l.id,b=void 0!==x?x:i.id;this.id=b,this.logger=y,this.clock=v,this.parent=g,this.options=l,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 l=i(this.eventListeners),p=l.next();!p.done;p=l.next()){(0,p.value)(t.event)}}catch(t){n={error:t}}finally{try{p&&!p.done&&(r=l.return)&&r.call(l)}finally{if(n)throw n.error}}try{for(var v=i(this.listeners),y=v.next();!y.done;y=v.next()){(0,y.value)(t,t.event)}}catch(t){o={error:t}}finally{try{y&&!y.done&&(s=v.return)&&s.call(v)}finally{if(o)throw o.error}}try{for(var g=i(this.contextListeners),m=g.next();!m.done;m=g.next()){(0,m.value)(this.state.context,this.state.history?this.state.history.context:void 0)}}catch(t){a={error:t}}finally{try{m&&!m.done&&(c=g.return)&&c.call(g)}finally{if(a)throw a.error}}if(this.state.done){var x=t.configuration.find((function(t){return"final"===t.type&&t.parent===h.machine})),b=x&&x.doneData?I(x.doneData,t.context,e):void 0;this._doneEvent=U(this.id,b);try{for(var S=i(this.doneListeners),w=S.next();!w.done;w=S.next()){(0,w.value)(this._doneEvent)}}catch(t){u={error:t}}finally{try{w&&!w.done&&(f=S.return)&&f.call(S)}finally{if(u)throw u.error}}this._stop(),this._stopChildren(),nt(this.sessionId)}},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=R(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(e){return this.status===t.InterpreterStatus.Stopped&&this._doneEvent?e(this._doneEvent):this.doneListeners.add(e),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){k(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 S=i(Object.keys(this.delayedEventsMap)),w=S.next();!w.done;w=S.next()){var _=w.value;this.clock.clearTimeout(this.delayedEventsMap[_])}}catch(t){f={error:t}}finally{try{w&&!w.done&&(h=S.return)&&h.call(S)}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=M({type:"xstate.stop"}),r=H(t,(function(){var r=E(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,[{type:"exit",actions: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!function(t){return(t.type===f||t.type===h&&t.to===n.Internal)&&"number"!=typeof t.delay}(t)})),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=M(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,M(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=M(t);if(0===r.name.indexOf(g)&&!this.state.nextEvents.some((function(t){return 0===t.indexOf(g)})))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,n=this.clock.setTimeout((function(){"to"in t&&t.to?e.sendTo(t._event,t.to,!0):e.send(t._event)}),t.delay);t.id&&(this.delayedEventsMap[t.id]=n)},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),k(e.stop)&&e.stop())},a.prototype.spawn=function(e,n,i){if(this.status!==t.InterpreterStatus.Running)return K(e,n);if(T(e))return this.spawnPromise(Promise.resolve(e),n);if(k(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&&k(t.subscribe)}catch(t){return!1}}(e))return this.spawnObservable(e,n);if(P(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(x,{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(M(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(M(U(e,t),{origin:e})))}),(function(t){if(!o){i.removeChild(e);var n=$(e,t);try{i.send(M(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=R(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}})[C]=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(M(t,{origin:e}))}),(function(t){a.add(t)}))}catch(t){this.send($(e,t))}if(T(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=R(t);return c.add(e.next),{unsubscribe:function(){c.delete(e.next)}}},stop:function(){s=!0,k(i)&&i()},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[C]=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(M(t,{origin:e}))}),(function(t){i.removeChild(e),i.send(M($(e,t),{origin:e}))}),(function(){i.removeChild(e),i.send(M(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}}})[C]=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}}})[C]=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[C]=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 j(t)?r(r({},at),{name:t}):r(r(r({},at),{name:N()}),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})}));