UNPKG

@urql/devtools

Version:
3 lines (2 loc) 3.49 kB
import{parse as e}from"graphql";import{pipe as n,tap as r,take as t,toPromise as o}from"wonka";function a(){return(a=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e}).apply(this,arguments)}var i=function(e){return{type:"debug-event",source:"exchange",data:a(a({},e),{source:"devtoolsExchange",timestamp:Date.now()})}},s=function(){var e=Error.stackTraceLimit;Error.stackTraceLimit=Infinity;var n=(new Error).stack||"";Error.stackTraceLimit=e;var r=/(useQuery|useMutation|useSubscription).*\n\s*at (\w+)/.exec(n);return null===r?"Unknown":r[2]},u={source:"exchange",type:"connection-init",version:"2.0.3"};function c(e){return window.postMessage(JSON.parse(JSON.stringify(e)),window.location.origin)}var d,f,v={"execute-query":function(r){var a=r.client;return function(r){var i,s=/(^|\W)+mutation\W/.test(r.query),u=a.createRequestOperation(s?"mutation":"query",{key:(i=JSON.stringify(r.query),function(e,n){e|=0;for(var r=0,t=0|n.length;r<t;r++)e=(e<<5)+e+n.charCodeAt(r);return e}(5381,i)>>>0),query:e(r.query)},{meta:{source:"Devtools"}});n(a.executeRequestOperation(u),t(1),o)}},"connection-init":function(e){var n=e.sendMessage;return function(){return n({type:"connection-acknowledge",source:"exchange",version:"2.0.3"})}}},g=(f=!(d="undefined"!=typeof navigator&&"ReactNative"===(null===navigator||void 0===navigator?void 0:navigator.product))&&"undefined"==typeof window,"production"===process.env.NODE_ENV||f?function(e){var r=e.forward;return function(e){return n(e,r)}}:function(e){var t=e.sendMessage,o=e.addMessageListener;function a(e){return t({type:"debug-event",source:"exchange",data:e})}return function(e){var u=e.client,c=e.forward;return o((function(e){"devtools"===e.source&&e.type in v&&v[e.type]({client:u,sendMessage:t})(e)})),u.subscribeToDebugTarget&&u.subscribeToDebugTarget(a),function(e){return n(e,r(function(e){var n=e.sendMessage;return function(e){if("teardown"===e.kind){var r=i({type:"teardown",message:"The operation has been torn down",operation:e,data:void 0});return n(r)}var t=i({type:"execution",message:"The client has received an execute command.",operation:e,data:{sourceComponent:s()}});return n(t)}}({client:u,sendMessage:t})),c,r(function(e){var n=e.sendMessage;return function(e){var r=e.operation,t=e.data,o=e.error;if(o){var a=i({type:"error",message:"The operation has returned a new error.",operation:r,data:{value:o}});return n(a)}var s=i({type:"update",message:"The operation has returned a new response.",operation:r,data:{value:t}});n(s)}}({client:u,sendMessage:t})))}}}(d?function(){var e,n,r=[];function t(){e.send(JSON.stringify(u))}function o(){n=n||setTimeout(s,500)}function a(){n=n||setTimeout(s,500)}function i(e){try{if(!e.data)return;r.forEach((function(n){return n(JSON.parse(e.data))}))}catch(e){console.warn(e)}}var s=function(){n=void 0,(e=new WebSocket("ws://localhost:7700")).onopen=t,e.onclose=o,e.onerror=a,e.onmessage=i};return s(),{addMessageListener:function(e){r=r.concat([e])},sendMessage:function(n){e.readyState===e.OPEN&&e.send(JSON.stringify(n))}}}():function(){var e=[];window.addEventListener("message",(function(n){var r=n.data;n.isTrusted&&(null==r?void 0:r.source)&&e.forEach((function(e){return e(r)}))}));var n=c;return n(u),{addMessageListener:function(n){return e=e.concat([n])},sendMessage:n}}()));export{g as devtoolsExchange}; //# sourceMappingURL=urql-devtools-exchange.es.min.js.map