UNPKG

25.5 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,u={};function c(t,e){return"object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n&&"_event"in n?t.value:d(t)?h(t):"string"!=typeof t?t:h(function(t,e){try{return d(t)?t:t.toString().split(e)}catch(e){throw new Error("'".concat(t,"' is not a valid state path."))}}(t,e));var n}function h(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 f(t,e,n){var r,o;if(p(t))return t(e,n.data);var s={};try{for(var a=i(Object.keys(t)),u=a.next();!u.done;u=a.next()){var c=u.value,h=t[c];p(h)?s[c]=h(e,n.data):s[c]=h}}catch(t){r={error:t}}finally{try{u&&!u.done&&(o=a.return)&&o.call(a)}finally{if(r)throw r.error}}return s}function l(t){return t instanceof Promise||!(null===t||!p(t)&&"object"!=typeof t||!p(t.then))}function d(t){return Array.isArray(t)}function p(t){return"function"==typeof t}function v(t){return"string"==typeof t}var y=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function g(t){return!!t&&"__xstatenode"in t}(a={})[y]=function(){return this},a[Symbol.observable]=function(){return this};var b=function(){var t=0;return function(){return(++t).toString(16)}}();function m(t,e){return v(t)||"number"==typeof t?r({type:t},e):t}function x(t,e){if(!v(t)&&"$$type"in t&&"scxml"===t.$$type)return t;var n=m(t);return r({name:n.type,data:n,$$type:"scxml",type:"external"},e)}function w(t,e,n){if("object"==typeof t)return t;var r=function(){};return{next:t,error:e||r,complete:n||r}}function S(t){return Object.keys(t.states).map((function(e){return t.states[e]})).filter((function(t){return"history"!==t.type}))}function E(t){return s([],o(new Set(function(t){var e;return(e=[]).concat.apply(e,s([],o(t),!1))}(s([],o(t.map((function(t){return t.ownEvents}))),!1)))),!1)}function _(t,e){return"compound"===e.type?S(e).some((function(e){return"final"===e.type&&(n=t,r=e,Array.isArray(n)?n.some((function(t){return t===r})):n instanceof Set&&n.has(r));var n,r})):"parallel"===e.type&&S(e).every((function(e){return _(t,e)}))}var O=e.Start,T=e.Stop;e.Raise;var I=e.Send,L=e.Cancel;e.NullEvent,e.Assign,e.After,e.DoneState;var k=e.Log,j=e.Init;e.Invoke,e.ErrorExecution;var C=e.ErrorPlatform,P=e.ErrorCustom,N=e.Update;e.Choose,e.Pure;var A=x({type:j});function D(t,n){var r="".concat(e.DoneInvoke,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}function M(t,n){var r="".concat(e.ErrorPlatform,".").concat(t),i={type:r,data:n,toString:function(){return r}};return i}var z=function(){function t(t){var e,n,r=this;this.actions=[],this.activities=u,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||u,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 E(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:A,_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=A;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="."),v(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=c(e,r),o=c(n,r);return v(o)?!!v(i)&&o===i:v(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}(),R={deferEvents:!1},V=function(){function t(t){this.processingEvent=!1,this.queue=[],this.initialized=!1,this.options=r(r({},R),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}(),J=[],q=function(t,e){J.push(t);var n=e(t);return J.pop(),n};function U(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}}})[y]=function(){return this},e}(e);if(r.deferred=!0,g(t)){var i=r.state=q(void 0,(function(){return(n?t.withContext(n):t).initialState}));r.getSnapshot=function(){return i}}return r}var $=new Map,F=0,X=function(){return"x:".concat(F++)},B=function(t,e){return $.set(t,e),t},G=function(t){return $.get(t)},H=function(t){$.delete(t)};function K(){return"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0}function Q(t){if(K()){var e=function(){var t=K();if(t&&"__xstate__"in t)return t.__xstate__}();e&&e.register(t)}}function W(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(){}})[y]=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,u),i.forEach((function(t){return t.next(n)}))}s=!1}}()},getSnapshot:function(){return n},subscribe:function(t,e,r){var o=w(t,e,r);return i.add(o),o.next(n),{unsubscribe:function(){i.delete(o)}}}}),u={parent:e.parent,self:a,id:e.id||"anonymous",observers:i};return n=t.start?t.start(u):n,a}var Y,Z={sync:!1,autoForward:!1};t.InterpreterStatus=void 0,(Y=t.InterpreterStatus||(t.InterpreterStatus={}))[Y.NotStarted=0]="NotStarted",Y[Y.Running=1]="Running",Y[Y.Stopped=2]="Stopped";var tt=function(){function a(e,i){var o=this;void 0===i&&(i=a.defaultOptions),this.machine=e,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.init=this.start,this.send=function(e,n){if(d(e))return o.batch(e),o.state;var r=x(m(e,n));if(o.status===t.InterpreterStatus.Stopped)return o.state;if(o.status!==t.InterpreterStatus.Running&&!o.options.deferEvents)throw new Error('Event "'.concat(r.name,'" was sent to uninitialized service "').concat(o.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 o.scheduler.schedule((function(){o.forward(r);var t=o.nextState(r);o.update(t,r)})),o._state},this.sendTo=function(t,e){var i,s=o.parent&&(e===n.Parent||o.parent.id===e),a=s?o.parent:v(e)?o.children.get(e)||G(e):(i=e)&&"function"==typeof i.send?e:void 0;if(a)"machine"in a?a.send(r(r({},t),{name:t.name===P?"".concat(M(o.id)):t.name,origin:o.sessionId})):a.send(t.data);else if(!s)throw new Error("Unable to send event to child '".concat(e,"' from service '").concat(o.id,"'."))};var s=r(r({},a.defaultOptions),i),u=s.clock,c=s.logger,h=s.parent,f=s.id,l=void 0!==f?f:e.id;this.id=l,this.logger=c,this.clock=u,this.parent=h,this.options=s,this.scheduler=new V({deferEvents:this.options.deferEvents}),this.sessionId=X()}return Object.defineProperty(a.prototype,"initialState",{get:function(){var t=this;return this._initialState?this._initialState:q(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,u,c,h,l=this;if(t._sessionid=this.sessionId,this._state=t,this.options.execute&&this.execute(this.state),this.children.forEach((function(t){l.state.children[t.id]=t})),this.devTools&&this.devTools.send(e.data,t),t.event)try{for(var d=i(this.eventListeners),p=d.next();!p.done;p=d.next()){(0,p.value)(t.event)}}catch(t){n={error:t}}finally{try{p&&!p.done&&(r=d.return)&&r.call(d)}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),b=g.next();!b.done;b=g.next()){(0,b.value)(this.state.context,this.state.history?this.state.history.context:void 0)}}catch(t){a={error:t}}finally{try{b&&!b.done&&(u=g.return)&&u.call(g)}finally{if(a)throw a.error}}var m=_(t.configuration||[],this.machine);if(this.state.configuration&&m){var x=t.configuration.find((function(t){return"final"===t.type&&t.parent===l.machine})),w=x&&x.doneData?f(x.doneData,t.context,e):void 0;try{for(var S=i(this.doneListeners),E=S.next();!E.done;E=S.next()){(0,E.value)(D(this.id,w))}}catch(t){c={error:t}}finally{try{E&&!E.done&&(h=S.return)&&h.call(S)}finally{if(c)throw c.error}}this.stop()}},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,o=this;if(!e)return{unsubscribe:function(){}};var s=r;return"function"==typeof e?i=e:(i=e.next.bind(e),s=e.complete.bind(e)),this.listeners.add(i),this.status!==t.InterpreterStatus.NotStarted&&i(this.state),s&&(this.status===t.InterpreterStatus.Stopped?s():this.onDone(s)),{unsubscribe:function(){i&&o.listeners.delete(i),s&&o.doneListeners.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(),B(this.sessionId,this),this.initialized=!0,this.status=t.InterpreterStatus.Running;var r=void 0===e?this.initialState:q(this,(function(){return"object"==typeof(t=e)&&null!==t&&"value"in t&&"_event"in t?n.machine.resolveState(e):n.machine.resolveState(z.from(e,n.machine.context));var t}));return this.options.devTools&&this.attachDev(),this.scheduler.initialize((function(){n.update(r,A)})),this},a.prototype.stop=function(){var e,n,r,a,u,c,h,f,l,d,v=this;try{for(var y=i(this.listeners),g=y.next();!g.done;g=y.next()){var b=g.value;this.listeners.delete(b)}}catch(t){e={error:t}}finally{try{g&&!g.done&&(n=y.return)&&n.call(y)}finally{if(e)throw e.error}}try{for(var m=i(this.stopListeners),x=m.next();!x.done;x=m.next()){(b=x.value)(),this.stopListeners.delete(b)}}catch(t){r={error:t}}finally{try{x&&!x.done&&(a=m.return)&&a.call(m)}finally{if(r)throw r.error}}try{for(var w=i(this.contextListeners),S=w.next();!S.done;S=w.next()){b=S.value;this.contextListeners.delete(b)}}catch(t){u={error:t}}finally{try{S&&!S.done&&(c=w.return)&&c.call(w)}finally{if(u)throw u.error}}try{for(var E=i(this.doneListeners),_=E.next();!_.done;_=E.next()){b=_.value;this.doneListeners.delete(b)}}catch(t){h={error:t}}finally{try{_&&!_.done&&(f=E.return)&&f.call(E)}finally{if(h)throw h.error}}if(!this.initialized)return this;s([],o(this.state.configuration),!1).sort((function(t,e){return e.order-t.order})).forEach((function(t){var e,n;try{for(var r=i(t.definition.exit),o=r.next();!o.done;o=r.next()){var s=o.value;v.exec(s,v.state)}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(e)throw e.error}}})),this.children.forEach((function(t){p(t.stop)&&t.stop()})),this.children.clear();try{for(var O=i(Object.keys(this.delayedEventsMap)),T=O.next();!T.done;T=O.next()){var I=T.value;this.clock.clearTimeout(this.delayedEventsMap[I])}}catch(t){l={error:t}}finally{try{T&&!T.done&&(d=O.return)&&d.call(O)}finally{if(l)throw l.error}}return this.scheduler.clear(),this.scheduler=new V({deferEvents:this.options.deferEvents}),this.initialized=!1,this.status=t.InterpreterStatus.Stopped,this._initialState=void 0,H(this.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.'));this.scheduler.schedule((function(){var t,a,u=n.state,c=!1,h=[],f=function(t){var e=x(t);n.forward(e),u=q(n,(function(){return n.machine.transition(u,e)})),h.push.apply(h,s([],o(u.actions.map((function(t){return n=u,i=(e=t).exec,r(r({},e),{exec:void 0!==i?function(){return i(n.context,n.event,{action:e,state:n,_event:n._event})}:void 0});var e,n,i}))),!1)),c=c||!!u.changed};try{for(var l=i(e),d=l.next();!d.done;d=l.next()){f(d.value)}}catch(e){t={error:e}}finally{try{d&&!d.done&&(a=l.return)&&a.call(l)}finally{if(t)throw t.error}}u.changed=c,u.actions=h,n.update(u,x(e[e.length-1]))}))},a.prototype.sender=function(t){return this.send.bind(this,t)},a.prototype.nextState=function(t){var e=this,n=x(t);if(0===n.name.indexOf(C)&&!this.state.nextEvents.some((function(t){return 0===t.indexOf(C)})))throw n.data.data;return q(this,(function(){return e.machine.transition(e.state,n)}))},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):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(n,r,i){void 0===i&&(i=this.machine.options.actions);var o,s=r.context,a=r._event,u=n.exec||function(t,e){return e&&e[t]||void 0}(n.type,i),c=p(u)?u:u?u.exec:n.exec;if(c)try{return c(s,a.data,{action:n,state:this.state,_event:a})}catch(t){throw this.parent&&this.parent.send({type:"xstate.error",data:t}),t}switch(n.type){case I:var h=n;if("number"==typeof h.delay)return void this.defer(h);h.to?this.sendTo(h._event,h.to):this.send(h._event);break;case L:this.cancel(n.sendId);break;case O:if(this.status!==t.InterpreterStatus.Running)return;var l=n.activity;if(!this.state.activities[l.id||l.type])break;if(l.type===e.Invoke){var d="string"==typeof(o=l.src)?{type:o}:o,v=this.machine.options.services?this.machine.options.services[d.type]:void 0,y=l.id,b=l.data,m="autoForward"in l?l.autoForward:!!l.forward;if(!v)return;var x=b?f(b,s,a):void 0;if("string"==typeof v)return;var w=p(v)?v(s,a.data,{data:x,src:d,meta:l.meta}):v;if(!w)return;var S=void 0;g(w)&&(w=x?w.withContext(x):w,S={autoForward:m}),this.spawn(w,y,S)}else this.spawnActivity(l);break;case T:this.stopChild(n.activity.id);break;case k:var E=n.label,_=n.value;E?this.logger(E,_):this.logger(_)}},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),p(e.stop)&&e.stop())},a.prototype.spawn=function(t,e,n){if(l(t))return this.spawnPromise(Promise.resolve(t),e);if(p(t))return this.spawnCallback(t,e);if(function(t){try{return"function"==typeof t.send}catch(t){return!1}}(o=t)&&"id"in o)return this.spawnActor(t,e);if(function(t){try{return"subscribe"in t&&p(t.subscribe)}catch(t){return!1}}(t))return this.spawnObservable(t,e);if(g(t))return this.spawnMachine(t,r(r({},n),{id:e}));if(null!==(i=t)&&"object"==typeof i&&"transition"in i&&"function"==typeof i.transition)return this.spawnBehavior(t,e);throw new Error('Unable to spawn entity "'.concat(e,'" of type "').concat(typeof t,'".'));var i,o},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({},Z),e);o.sync&&i.onTransition((function(t){n.send(N,{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(x(t,{origin:i.id}))})).start(),s},a.prototype.spawnBehavior=function(t,e){var n=W(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(x(D(e,t),{origin:e})))}),(function(t){if(!o){i.removeChild(e);var n=M(e,t);try{i.send(x(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=w(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}})[y]=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,u=new Set;try{i=t((function(t){r=t,u.forEach((function(e){return e(t)})),s||o.send(x(t,{origin:e}))}),(function(t){a.add(t)}))}catch(t){this.send(M(e,t))}if(l(i))return this.spawnPromise(i,e);var c=((n={id:e,send:function(t){return a.forEach((function(e){return e(t)}))},subscribe:function(t){var e=w(t);return u.add(e.next),{unsubscribe:function(){u.delete(e.next)}}},stop:function(){s=!0,p(i)&&i()},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[y]=function(){return this},n);return this.children.set(e,c),c},a.prototype.spawnObservable=function(t,e){var n,r,i=this,o=t.subscribe((function(t){r=t,i.send(x(t,{origin:e}))}),(function(t){i.removeChild(e),i.send(x(M(e,t),{origin:e}))}),(function(){i.removeChild(e),i.send(x(D(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}}})[y]=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}}})[y]=function(){return this},n))},a.prototype.attachDev=function(){var t=K();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)}Q(this)}},a.prototype.toJSON=function(){return{id:this.id}},a.prototype[y]=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=et,a}();function et(t,e){return new tt(t,e)}t.Interpreter=tt,t.interpret=et,t.spawn=function(t,e){var n=function(t){return v(t)?r(r({},Z),{name:t}):r(r(r({},Z),{name:b()}),t)}(e);return function(e){return e?e.spawn(t,n.name,n):U(t,n.name)}(J[J.length-1])},Object.defineProperty(t,"__esModule",{value:!0})}));