UNPKG

@xstate/inspect

Version:
16 lines (13 loc) 10.6 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("xstate")):"function"==typeof define&&define.amd?define(["exports","xstate"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).XStateInspect={},e.XState)}(this,(function(e,t){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var n=function(){return(n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};var r=a;a.default=a,a.stable=d,a.stableStringify=d;var i=[],o=[];function s(){return{depthLimit:Number.MAX_SAFE_INTEGER,edgesLimit:Number.MAX_SAFE_INTEGER}}function a(e,t,n,r){var a;void 0===r&&(r=s()),function e(t,n,r,i,o,s,a){var u;if(s+=1,"object"==typeof t&&null!==t){for(u=0;u<i.length;u++)if(i[u]===t)return void c("[Circular]",t,n,o);if(void 0!==a.depthLimit&&s>a.depthLimit)return void c("[...]",t,n,o);if(void 0!==a.edgesLimit&&r+1>a.edgesLimit)return void c("[...]",t,n,o);if(i.push(t),Array.isArray(t))for(u=0;u<t.length;u++)e(t[u],u,u,i,t,s,a);else{var d=Object.keys(t);for(u=0;u<d.length;u++){var f=d[u];e(t[f],f,u,i,t,s,a)}}i.pop()}}(e,"",0,[],void 0,0,r);try{a=0===o.length?JSON.stringify(e,t,n):JSON.stringify(e,f(t),n)}catch(e){return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]")}finally{for(;0!==i.length;){var u=i.pop();4===u.length?Object.defineProperty(u[0],u[1],u[3]):u[0][u[1]]=u[2]}}return a}function c(e,t,n,r){var s=Object.getOwnPropertyDescriptor(r,n);void 0!==s.get?s.configurable?(Object.defineProperty(r,n,{value:e}),i.push([r,n,t,s])):o.push([t,n,e]):(r[n]=e,i.push([r,n,t]))}function u(e,t){return e<t?-1:e>t?1:0}function d(e,t,n,r){void 0===r&&(r=s());var a,d=function e(t,n,r,o,s,a,d){var f;if(a+=1,"object"==typeof t&&null!==t){for(f=0;f<o.length;f++)if(o[f]===t)return void c("[Circular]",t,n,s);try{if("function"==typeof t.toJSON)return}catch(e){return}if(void 0!==d.depthLimit&&a>d.depthLimit)return void c("[...]",t,n,s);if(void 0!==d.edgesLimit&&r+1>d.edgesLimit)return void c("[...]",t,n,s);if(o.push(t),Array.isArray(t))for(f=0;f<t.length;f++)e(t[f],f,f,o,t,a,d);else{var l={},v=Object.keys(t).sort(u);for(f=0;f<v.length;f++){var p=v[f];e(t[p],p,f,o,t,a,d),l[p]=t[p]}if(void 0===s)return l;i.push([s,n,t]),s[n]=l}o.pop()}}(e,"",0,[],void 0,0,r)||e;try{a=0===o.length?JSON.stringify(d,t,n):JSON.stringify(d,f(t),n)}catch(e){return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]")}finally{for(;0!==i.length;){var l=i.pop();4===l.length?Object.defineProperty(l[0],l[1],l[3]):l[0][l[1]]=l[2]}}return a}function f(e){return e=void 0!==e?e:function(e,t){return t},function(t,n){if(o.length>0)for(var r=0;r<o.length;r++){var i=o[r];if(i[1]===t&&i[0]===n){n=i[2],o.splice(r,1);break}}return e.call(this,t,n)}}function l(e){return"function"==typeof e?e():e}function v(e,t){try{return JSON.stringify(e,t)}catch(n){return r(e,t)}}function p(e){if(!e)return!1;try{if("object"==typeof e&&"type"in e&&e.type.startsWith("service."))return!0}catch(e){return!1}return!1}function y(e){var n=t.State.create(JSON.parse(e));return delete n.history,n}function g(e){switch(e.type){case"service.event":return n(n({},e),{event:JSON.parse(e.event)});case"service.register":return n(n({},e),{machine:t.createMachine(JSON.parse(e.machine)),state:y(e.state)});case"service.state":return n(n({},e),{state:y(e.state)});default:return e}}function h(e,t,r){var i,o,s={};try{for(var a=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),c=a.next();!c.done;c=a.next()){var u=c.value;s[u]=e[u]}}catch(e){i={error:e}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(i)throw i.error}}var d=JSON.parse(v(s,r));return v(n(n({},e),d))}function b(e,t){return e.machine,e.configuration,e.history,h(function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(e);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(e,r[i])&&(n[r[i]]=e[r[i]])}return n}(e,["machine","configuration","history"]),["context","event","_event"],t)}function w(e,t){return h(e.definition,["context"],t)}var m=new Map;function S(){var e=new Set,t=new Set;return{services:e,register:function(n){e.add(n),m.set(n.sessionId,n),t.forEach((function(e){return e(n)})),n.onStop((function(){e.delete(n),m.delete(n.sessionId)}))},unregister:function(t){e.delete(t),m.delete(t.sessionId)},onRegister:function(n){return t.add(n),e.forEach((function(e){return n(e)})),{unsubscribe:function(){t.delete(n)}}}}}var O={url:"https://stately.ai/viz?inspect",iframe:function(){return document.querySelector("iframe[data-xstate]")},devTools:function(){var e=S();return globalThis.__xstate__=e,e},serialize:void 0,targetWindow:void 0},x=new Set;e.createDevTools=S,e.createWebSocketReceiver=function(e){var n,r=e.protocol,i=new WebSocket("".concat(void 0===r?"ws":r,"://").concat(e.server)),o=new Set,s=t.toActorRef({id:"xstate.webSocketReceiver",send:function(t){i.send(v(t,e.serialize))},subscribe:function(e,n,r){var i=t.toObserver(e,n,r);return o.add(i),{unsubscribe:function(){o.delete(i)}}},getSnapshot:function(){return n}});return i.onopen=function(){s.send({type:"xstate.inspecting"})},i.onmessage=function(e){if("string"==typeof e.data)try{var t=JSON.parse(e.data);p(t)&&(n=g(t),o.forEach((function(e){e.next(n)})))}catch(e){console.error(e)}},i.onerror=function(e){o.forEach((function(t){var n;null===(n=t.error)||void 0===n||n.call(t,e)}))},s},e.createWindowReceiver=function(e){var n,r=e||{},i=r.window,o=void 0===i?window:i,s=r.targetWindow,a=void 0===s?window.self===window.top?window.opener:window.parent:s,c=new Set,u=function(e){var t=e.data;p(t)&&(n=g(t),c.forEach((function(e){return e.next(n)})))};o.addEventListener("message",u);var d=t.toActorRef({id:"xstate.windowReceiver",send:function(e){a&&a.postMessage(e,"*")},subscribe:function(e,n,r){var i=t.toObserver(e,n,r);return c.add(i),{unsubscribe:function(){c.delete(i)}}},stop:function(){c.clear(),o.removeEventListener("message",u)},getSnapshot:function(){return n}});return d.send({type:"xstate.inspecting"}),d},e.inspect=function(e){var r=function(e){var t=n(n({},O),e);return n(n({},t),{url:new URL(t.url),iframe:l(t.iframe),devTools:l(t.devTools)})}(e),i=r.iframe,o=r.url,s=r.devTools;if(null===(null==e?void 0:e.targetWindow))throw new Error("Received a nullable `targetWindow`.");var a=r.targetWindow;if(null!==i||a){var c,u=function(e,n){void 0===e&&(e=globalThis.__xstate__);var r=new Map,i=e.onRegister((function(e){r.set(e.sessionId,e)}));return t.createMachine({predictableActionArguments:!0,initial:"pendingConnection",context:{client:void 0},states:{pendingConnection:{},connected:{on:{"service.state":{actions:function(e,t){return e.client.send(t)}},"service.event":{actions:function(e,t){return e.client.send(t)}},"service.register":{actions:function(e,t){return e.client.send(t)}},"service.stop":{actions:function(e,t){return e.client.send(t)}},"xstate.event":{actions:function(e,t){var n=t.event,i=JSON.parse(n),o=r.get(i.origin);null==o||o.send(i)}},unload:{actions:function(e){e.client.send({type:"xstate.disconnect"})}},disconnect:"disconnected"}},disconnected:{entry:function(){i.unsubscribe()},type:"final"}},on:{"xstate.inspecting":{target:".connected",actions:[t.assign({client:function(e,t){return t.client}}),function(t){e.services.forEach((function(e){var r;null===(r=t.client)||void 0===r||r.send({type:"service.register",machine:w(e.machine,null==n?void 0:n.serialize),state:b(e.state||e.initialState,null==n?void 0:n.serialize),sessionId:e.sessionId})}))}]}}})}(s,e),d=t.interpret(u).start(),f=new Set,p=d.subscribe((function(e){f.forEach((function(t){return t.next(e)}))})),y=function(e){if("object"==typeof e.data&&null!==e.data&&"type"in e.data){i&&!a&&(a=i.contentWindow),c||(c={send:function(e){a.postMessage(e,o.origin)}});var t=n(n({},e.data),{client:c});d.send(t)}};window.addEventListener("message",y),window.addEventListener("unload",(function(){d.send({type:"unload"})}));var g=function(t){return v(t,null==e?void 0:e.serialize)};return s.onRegister((function(n){var r,i=n.state||n.initialState;if(d.send({type:"service.register",machine:w(n.machine,null==e?void 0:e.serialize),state:b(i,null==e?void 0:e.serialize),sessionId:n.sessionId,id:n.id,parent:null===(r=n.parent)||void 0===r?void 0:r.sessionId}),d.send({type:"service.event",event:g(i._event),sessionId:n.sessionId}),!x.has(n)){x.add(n);var o=n.send.bind(n);n.send=function(e,r){return d.send({type:"service.event",event:g(t.toSCXMLEvent(t.toEventObject(e,r))),sessionId:n.sessionId}),o(e,r)}}n.subscribe((function(t){void 0!==t&&d.send({type:"service.state",state:b(t,null==e?void 0:e.serialize),sessionId:n.sessionId})})),n.onStop((function(){d.send({type:"service.stop",sessionId:n.sessionId})}))})),i?(i.addEventListener("load",(function(){a=i.contentWindow})),i.setAttribute("src",String(o))):a||(a=window.open(String(o),"xstateinspector")),{send:function(e){d.send(e)},subscribe:function(e,n,r){var i=t.toObserver(e,n,r);return f.add(i),i.next(d.state),{unsubscribe:function(){f.delete(i)}}},disconnect:function(){d.send("disconnect"),window.removeEventListener("message",y),p.unsubscribe()}}}console.warn("No suitable <iframe> found to embed the inspector. Please pass an <iframe> element to `inspect(iframe)` or create an <iframe data-xstate></iframe> element.")},Object.defineProperty(e,"__esModule",{value:!0})}));