Files
30-seconds-of-code/node_modules/xstate/dist/xstate.interpreter.js
2019-08-20 15:52:05 +02:00

2 lines
17 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).XStateInterpreter={})}(this,function(t){"use strict";var e,n,i=function(){return(i=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function r(t){var e="function"==typeof Symbol&&t[Symbol.iterator],n=0;return e?e.call(t):{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}}}function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var i,r,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(i=o.next()).done;)s.push(i.value)}catch(t){r={error:t}}finally{try{i&&!i.done&&(n=o.return)&&n.call(o)}finally{if(r)throw r.error}}return s}function s(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(o(arguments[e]));return t}!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.Update="xstate.update",t.Pure="xstate.pure"}(e||(e={})),function(t){t.Parent="#_parent",t.Internal="#_internal"}(n||(n={}));var a=".",c={};function u(t){return!y(t)&&("value"in t&&"tree"in t&&"history"in t)}function h(t){return Object.keys(t)}function f(t,e){return u(t)?t.value:v(t)?l(t):"string"==typeof t||u(t)?l(function(t,e){try{return v(t)?t:t.toString().split(e)}catch(e){throw new Error("'"+t+"' is not a valid state path.")}}(t,e)):t}function l(t){if(1===t.length)return t[0];for(var e={},n=e,i=0;i<t.length-1;i++)i===t.length-2?n[t[i]]=t[i+1]:(n[t[i]]={},n=n[t[i]]);return e}function d(t){return t instanceof Promise||!(null===t||!p(t)&&"object"!=typeof t||!p(t.then))}function v(t){return Array.isArray(t)}function p(t){return"function"==typeof t}function y(t){return"string"==typeof t}function w(t){try{return"subscribe"in t&&p(t.subscribe)}catch(t){return!1}}function x(t){try{return"__xstatenode"in t}catch(t){return!1}}var b,m=(b=0,function(){return(++b).toString(16)}),E=function(){function t(t){this.actions=[],this.activities=c,this.meta={},this.events=[],this.value=t.value,this.context=t.context,this.event=t.event,this.historyValue=t.historyValue,this.history=t.history,this.actions=t.actions||[],this.activities=t.activities||c,this.meta=t.meta||{},this.events=t.events||[],Object.defineProperty(this,"tree",{value:t.tree,enumerable:!1}),this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this)}return t.from=function(n,i){return n instanceof t?n.context!==i?new t({value:n.value,context:i,event:n.event,historyValue:n.historyValue,history:n.history,actions:[],activities:n.activities,meta:{},events:[],tree:n.tree}):n:new t({value:n,context:i,event:{type:e.Init},historyValue:void 0,history:void 0,actions:[],activities:void 0,meta:void 0,events:[]})},t.create=function(e){return new t(e)},t.inert=function(n,i){if(n instanceof t){if(!n.actions.length)return n;var r={type:e.Init};return new t({value:n.value,context:i,event:r,historyValue:n.historyValue,history:n.history,activities:n.activities,tree:n.tree})}return t.from(n,i)},Object.defineProperty(t.prototype,"inert",{get:function(){return t.inert(this,this.context)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nextEvents",{get:function(){return this.tree?this.tree.nextEvents:[]},enumerable:!0,configurable:!0}),t.prototype.toStrings=function(t,e){var n=this;if(void 0===t&&(t=this.value),void 0===e&&(e="."),y(t))return[t];var i=h(t);return i.concat.apply(i,s(i.map(function(i){return n.toStrings(t[i]).map(function(t){return i+e+t})})))},t.prototype.matches=function(t){return function t(e,n,i){void 0===i&&(i=a);var r=f(e,i),o=f(n,i);return y(o)?!!y(r)&&o===r:y(r)?r in o:h(r).every(function(e){return e in o&&t(r[e],o[e])})}(t,this.value)},t}(),g=e.Start,S=e.Stop,O=(e.Raise,e.Send),T=e.Cancel,L=(e.NullEvent,e.Assign,e.After,e.DoneState,e.Log),k=e.Init,P=(e.Invoke,e.ErrorExecution,e.ErrorPlatform),_=e.Update;function I(t,e){if(y(t)||"number"==typeof t){var n={type:t};return e&&Object.assign(n,e),n}return t}function z(t,n){var i=e.DoneInvoke+"."+t,r={type:i,data:n,toString:function(){return i}};return r}function D(t,n){var i=e.ErrorPlatform+"."+t,r={type:i,data:n,toString:function(){return i}};return r}var M={deferEvents:!1},N=function(){function t(t){this.processingEvent=!1,this.queue=[],this.initialized=!1,this.options=i({},M,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.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.queue=[],t}finally{this.processingEvent=!1}},t}();var j,C={sync:!1,autoForward:!1},U=(j=[],function(t,e){t&&j.push(t);var n=e(t||j[j.length-1]);return t&&j.pop(),n}),V=function(){function t(e,r){var o=this;void 0===r&&(r=t.defaultOptions),this.machine=e,this.scheduler=new N,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.children=new Map,this.forwardTo=new Set,this.init=this.start,this.send=function(t,e){if(v(t))return o.batch(t),o.state;var n=I(t,e);if(!o.initialized&&o.options.deferEvents);else if(!o.initialized)throw new Error('Event "'+n.type+'" was sent to uninitialized service "'+o.machine.id+'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: '+JSON.stringify(n));return o.scheduler.schedule(function(){var t=o.nextState(n);o.update(t,n),o.forward(n)}),o.state},this.sendTo=function(t,e){var i=e===n.Parent,r=i?o.parent:function(t){try{return"function"==typeof t.send}catch(t){return!1}}(e)?e:o.children.get(e);if(r)r.send(t);else if(!i)throw new Error("Unable to send event to child '"+e+"' from service '"+o.id+"'.")};var s=i({},t.defaultOptions,r),a=s.clock,c=s.logger,u=s.parent,h=s.id,f=void 0!==h?h:e.id;this.id=f,this.logger=c,this.clock=a,this.parent=u,this.options=s,this.scheduler=new N({deferEvents:this.options.deferEvents}),this.initialState=this.state=U(this,function(){return o.machine.initialState})}return t.prototype.execute=function(t,e){var n,i;try{for(var o=r(t.actions),s=o.next();!s.done;s=o.next()){var a=s.value;this.exec(a,t.context,t.event,e)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(i=o.return)&&i.call(o)}finally{if(n)throw n.error}}},t.prototype.update=function(t,e){var n,i,o,s,a,c,u,h;if(this.state=t,this.options.execute&&this.execute(this.state),this.devTools&&this.devTools.send(e,t),t.event)try{for(var f=r(this.eventListeners),l=f.next();!l.done;l=f.next()){(0,l.value)(t.event)}}catch(t){n={error:t}}finally{try{l&&!l.done&&(i=f.return)&&i.call(f)}finally{if(n)throw n.error}}try{for(var d=r(this.listeners),v=d.next();!v.done;v=d.next()){(0,v.value)(t,t.event)}}catch(t){o={error:t}}finally{try{v&&!v.done&&(s=d.return)&&s.call(d)}finally{if(o)throw o.error}}try{for(var p=r(this.contextListeners),y=p.next();!y.done;y=p.next()){(0,y.value)(this.state.context,this.state.history?this.state.history.context:void 0)}}catch(t){a={error:t}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(a)throw a.error}}if(this.state.tree&&this.state.tree.done){var w=this.state.tree.getDoneData(this.state.context,I(e));try{for(var x=r(this.doneListeners),b=x.next();!b.done;b=x.next()){(0,b.value)(z(this.id,w))}}catch(t){u={error:t}}finally{try{b&&!b.done&&(h=x.return)&&h.call(x)}finally{if(u)throw u.error}}this.stop()}},t.prototype.onTransition=function(t){return this.listeners.add(t),this},t.prototype.subscribe=function(t,e,n){var i=this;return t&&this.onTransition(t),n&&this.onDone(n),{unsubscribe:function(){t&&i.listeners.delete(t),n&&i.doneListeners.delete(n)}}},t.prototype.onEvent=function(t){return this.eventListeners.add(t),this},t.prototype.onSend=function(t){return this.sendListeners.add(t),this},t.prototype.onChange=function(t){return this.contextListeners.add(t),this},t.prototype.onStop=function(t){return this.stopListeners.add(t),this},t.prototype.onDone=function(t){return this.doneListeners.add(t),this},t.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},t.prototype.start=function(t){var e=this;if(this.initialized)return this;this.initialized=!0;var n=U(this,function(){return void 0===t?e.machine.initialState:t instanceof E?e.machine.resolveState(t):e.machine.resolveState(E.from(t))});return this.options.devTools&&this.attachDev(),this.scheduler.initialize(function(){e.update(n,{type:k})}),this},t.prototype.stop=function(){var t,e,n,i,o,s,a,c,u,f;try{for(var l=r(this.listeners),d=l.next();!d.done;d=l.next()){var v=d.value;this.listeners.delete(v)}}catch(e){t={error:e}}finally{try{d&&!d.done&&(e=l.return)&&e.call(l)}finally{if(t)throw t.error}}try{for(var y=r(this.stopListeners),w=y.next();!w.done;w=y.next()){(v=w.value)(),this.stopListeners.delete(v)}}catch(t){n={error:t}}finally{try{w&&!w.done&&(i=y.return)&&i.call(y)}finally{if(n)throw n.error}}try{for(var x=r(this.contextListeners),b=x.next();!b.done;b=x.next()){v=b.value;this.contextListeners.delete(v)}}catch(t){o={error:t}}finally{try{b&&!b.done&&(s=x.return)&&s.call(x)}finally{if(o)throw o.error}}try{for(var m=r(this.doneListeners),E=m.next();!E.done;E=m.next()){v=E.value;this.doneListeners.delete(v)}}catch(t){a={error:t}}finally{try{E&&!E.done&&(c=m.return)&&c.call(m)}finally{if(a)throw a.error}}this.children.forEach(function(t){p(t.stop)&&t.stop()});try{for(var g=r(h(this.delayedEventsMap)),S=g.next();!S.done;S=g.next()){var O=S.value;this.clock.clearTimeout(this.delayedEventsMap[O])}}catch(t){u={error:t}}finally{try{S&&!S.done&&(f=g.return)&&f.call(g)}finally{if(u)throw u.error}}return this.initialized=!1,this},t.prototype.batch=function(t){var e=this;if(!this.initialized&&this.options.deferEvents);else if(!this.initialized)throw new Error(t.length+' event(s) were sent to uninitialized service "'+this.machine.id+'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.');this.scheduler.schedule(function(){var n,o,a,c=e.state;try{for(var u=r(t),h=u.next();!h.done;h=u.next()){var f=h.value,l=c.changed,d=I(f),v=c.actions.map(function(t){return n=c,r=(e=t).exec,i({},e,{exec:void 0!==r?function(){return r(n.context,n.event,{action:e,state:n})}:void 0});var e,n,r});(a=(c=e.machine.transition(c,d)).actions).unshift.apply(a,s(v)),c.changed=c.changed||!!l,e.forward(d)}}catch(t){n={error:t}}finally{try{h&&!h.done&&(o=u.return)&&o.call(u)}finally{if(n)throw n.error}}e.update(c,I(t[t.length-1]))})},t.prototype.sender=function(t){return this.send.bind(this,t)},t.prototype.nextState=function(t){var e=this,n=I(t);if(0===n.type.indexOf(P)&&!this.state.nextEvents.some(function(t){return 0===t.indexOf(P)}))throw n.data;return U(this,function(){return e.machine.transition(e.state,n,e.state.context)})},t.prototype.forward=function(t){var e,n;try{for(var i=r(this.forwardTo),o=i.next();!o.done;o=i.next()){var s=o.value,a=this.children.get(s);if(!a)throw new Error("Unable to forward event '"+t+"' from interpreter '"+this.id+"' to nonexistant child '"+s+"'.");a.send(t)}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}},t.prototype.defer=function(t){var e=this,n=t.delay;if(y(n)){if(!this.machine.options.delays||void 0===this.machine.options.delays[n])return;var i=this.machine.options.delays[n];n="number"==typeof i?i:i(this.state.context,this.state.event)}this.delayedEventsMap[t.id]=this.clock.setTimeout(function(){t.to?e.sendTo(t.event,t.to):e.send(t.event)},n||0)},t.prototype.cancel=function(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]},t.prototype.exec=function(t,n,i,o){var s=function(t,e){return e&&e[t]||void 0}(t.type,o)||t.exec,a=p(s)?s:s?s.exec:t.exec;if(a)return a(n,i,{action:t,state:this.state});switch(t.type){case O:var c=t;if(c.delay)return void this.defer(c);c.to?this.sendTo(c.event,c.to):this.send(c.event);break;case T:this.cancel(t.sendId);break;case g:var u=t.activity;if(!this.state.activities[u.type])break;if(u.type===e.Invoke){var f=this.machine.options.services?this.machine.options.services[u.src]:void 0,l=u.id,v=u.data,y="autoForward"in u?u.autoForward:!!u.forward;if(!f)return;var b=p(f)?f(n,i):f;d(b)?this.spawnPromise(Promise.resolve(b),l):p(b)?this.spawnCallback(b,l):w(b)?this.spawnObservable(b,l):x(b)&&this.spawnMachine(v?b.withContext(function(t,e,n){var i,o;if(p(t))return t(e,n);var s={};try{for(var a=r(h(t)),c=a.next();!c.done;c=a.next()){var u=c.value,f=t[u];p(f)?s[u]=f(e,n):s[u]=f}}catch(t){i={error:t}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(i)throw i.error}}return s}(v,n,i)):b,{id:l,autoForward:y})}else this.spawnActivity(u);break;case S:this.stopChild(t.activity.id);break;case L:var m=t.expr?t.expr(n,i):void 0;t.label?this.logger(t.label,m):this.logger(m)}},t.prototype.stopChild=function(t){var e=this.children.get(t);e&&(this.children.delete(t),this.forwardTo.delete(t),p(e.stop)&&e.stop())},t.prototype.spawn=function(t,e,n){if(d(t))return this.spawnPromise(Promise.resolve(t),e);if(p(t))return this.spawnCallback(t,e);if(w(t))return this.spawnObservable(t,e);if(x(t))return this.spawnMachine(t,i({},n,{id:e}));throw new Error('Unable to spawn entity "'+e+'" of type "'+typeof t+'".')},t.prototype.spawnMachine=function(e,n){var r=this;void 0===n&&(n={});var o=new t(e,i({},this.options,{parent:this,id:n.id||e.id})),s=i({},C,n);s.sync&&o.onTransition(function(t){r.send(_,{state:t,id:o.id})}),o.onDone(function(t){r.send(t)}).start();var a=o;return this.children.set(o.id,a),s.autoForward&&this.forwardTo.add(o.id),a},t.prototype.spawnPromise=function(t,e){var n=this,i=!1;t.then(function(t){i||n.send(z(e,t))},function(t){if(!i){var r=D(e,t);try{n.send(r)}catch(i){n.reportUnhandledExceptionOnInvocation(t,i,e),n.devTools&&n.devTools.send(r,n.state),n.machine.strict&&n.stop()}}});var r={id:e,send:function(){},subscribe:function(e,n,i){var r=!1;return t.then(function(t){r||(e&&e(t),r||i&&i())},function(t){r||n(t)}),{unsubscribe:function(){return r=!0}}},stop:function(){i=!0},toJSON:function(){return{id:e}}};return this.children.set(e,r),r},t.prototype.spawnCallback=function(t,e){var n,i=this,r=!1,o=new Set;try{n=t(function(t){r||i.send(t)},function(t){o.add(t)})}catch(t){this.send(D(e,t))}if(d(n))return this.spawnPromise(n,e);var s={id:e,send:function(t){return o.forEach(function(e){return e(t)})},subscribe:function(t){return o.add(t),{unsubscribe:function(){o.delete(t)}}},stop:function(){r=!0,p(n)&&n()},toJSON:function(){return{id:e}}};return this.children.set(e,s),s},t.prototype.spawnObservable=function(t,e){var n=this,i=t.subscribe(function(t){n.send(t)},function(t){n.send(D(e,t))},function(){n.send(z(e))}),r={id:e,send:function(){},subscribe:function(e,n,i){return t.subscribe(e,n,i)},stop:function(){return i.unsubscribe()},toJSON:function(){return{id:e}}};return this.children.set(e,r),r},t.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)}},t.prototype.spawnEffect=function(t,e){this.children.set(t,{id:t,send:function(){},subscribe:function(){return{unsubscribe:function(){}}},stop:e||void 0,toJSON:function(){return{id:t}}})},t.prototype.reportUnhandledExceptionOnInvocation=function(t,e,n){},t.prototype.attachDev=function(){if(this.options.devTools&&"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){var t="object"==typeof this.options.devTools?this.options.devTools:void 0;this.devTools=window.__REDUX_DEVTOOLS_EXTENSION__.connect(i({name:this.id,autoPause:!0,stateSanitizer:function(t){return{value:t.value,context:t.context,actions:t.actions}}},t,{features:i({jump:!1,skip:!1},t?t.features:void 0)})),this.devTools.init(this.state)}},t.prototype.toJSON=function(){return{id:this.id}},t.defaultOptions=function(t){return{execute:!0,deferEvents:!0,clock:{setTimeout:function(e,n){return t.setTimeout.call(null,e,n)},clearTimeout:function(e){return t.clearTimeout.call(null,e)}},logger:t.console.log.bind(console),devTools:!1}}("undefined"==typeof window?global:window),t.interpret=J,t}(),A=function(t){return void 0===t&&(t="null"),{id:t,send:function(){},subscribe:function(){return{unsubscribe:function(){}}},toJSON:function(){return{id:t}}}},q=function(t){return y(t)?i({},C,{name:t}):i({},C,{name:m()},t)};function J(t,e){return new V(t,e)}t.Interpreter=V,t.spawn=function(t,e){var n=q(e);return U(void 0,function(e){return e?e.spawn(t,n.name,n):A(n.name)})},t.interpret=J,Object.defineProperty(t,"__esModule",{value:!0})});