|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @flow |
| 10 | + */ |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +/** |
| 14 | + * NOTE: This file cannot `require` any other modules. We `.toString()` the |
| 15 | + * function in some places and inject the source into the page. |
| 16 | + */ |
| 17 | +function installRelayHook(window: Object) { |
| 18 | + var hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__; |
| 19 | + if (!hook) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + function decorate(obj, attr, fn) { |
| 24 | + var old = obj[attr]; |
| 25 | + obj[attr] = function() { |
| 26 | + var res = old.apply(this, arguments); |
| 27 | + fn.apply(this, arguments); |
| 28 | + return res; |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + var _eventQueue = []; |
| 33 | + var _listener = null; |
| 34 | + function emit(name: string, data: mixed) { |
| 35 | + _eventQueue.push({name, data}); |
| 36 | + if (_listener) { |
| 37 | + _listener(name, data); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + function setRequestListener( |
| 42 | + listener: (name: string, data: mixed) => void |
| 43 | + ): () => void { |
| 44 | + if (_listener) { |
| 45 | + throw new Error( |
| 46 | + 'Relay Devtools: Called only call setRequestListener once.' |
| 47 | + ); |
| 48 | + } |
| 49 | + _listener = listener; |
| 50 | + _eventQueue.forEach(({name, data}) => { |
| 51 | + listener(name, data); |
| 52 | + }); |
| 53 | + |
| 54 | + return () => { |
| 55 | + _listener = null; |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + function recordRequest(type: 'mutation' | 'query', request) { |
| 60 | + var id = Math.random().toString(16).substr(2); |
| 61 | + request.then( |
| 62 | + response => { |
| 63 | + emit('relay:success', { |
| 64 | + id: id, |
| 65 | + end: Date.now(), |
| 66 | + response: response.response, |
| 67 | + }); |
| 68 | + }, |
| 69 | + error => { |
| 70 | + emit('relay:failure', { |
| 71 | + id: id, |
| 72 | + end: Date.now(), |
| 73 | + error: error, |
| 74 | + }); |
| 75 | + }, |
| 76 | + ); |
| 77 | + return { |
| 78 | + id: id, |
| 79 | + type: type, |
| 80 | + start: Date.now(), |
| 81 | + text: request.getQueryString(), |
| 82 | + variables: request.getVariables(), |
| 83 | + name: request.getDebugName(), |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + function instrumentRelayRequests(relayInternals: Object) { |
| 88 | + var NetworkLayer = relayInternals.NetworkLayer; |
| 89 | + |
| 90 | + decorate(NetworkLayer, 'sendMutation', mutation => { |
| 91 | + emit('relay:pending', [recordRequest('mutation', mutation)]); |
| 92 | + }); |
| 93 | + |
| 94 | + decorate(NetworkLayer, 'sendQueries', queries => { |
| 95 | + emit('relay:pending', queries.map(query => recordRequest('query', query))); |
| 96 | + }); |
| 97 | + |
| 98 | + var instrumented = {}; |
| 99 | + for (var key in relayInternals) { |
| 100 | + if (relayInternals.hasOwnProperty(key)) { |
| 101 | + instrumented[key] = relayInternals[key]; |
| 102 | + } |
| 103 | + } |
| 104 | + instrumented.setRequestListener = setRequestListener; |
| 105 | + return instrumented; |
| 106 | + } |
| 107 | + |
| 108 | + var _relayInternals = null; |
| 109 | + Object.defineProperty(hook, '_relayInternals', ({ |
| 110 | + set: function(relayInternals) { |
| 111 | + _relayInternals = instrumentRelayRequests(relayInternals); |
| 112 | + }, |
| 113 | + get: function() { |
| 114 | + return _relayInternals; |
| 115 | + }, |
| 116 | + }: any)); |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = installRelayHook; |
0 commit comments