|
| 1 | +// IMPORTANT: this script is injected into every page!!! |
| 2 | + |
| 3 | +/** |
| 4 | + * Install the hook on window, which is an event emitter. |
| 5 | + * Note because Chrome content scripts cannot directly modify the window object, |
| 6 | + * we are evaling this function by inserting a script tag. That's why we have |
| 7 | + * to inline the whole event emitter implementation here. |
| 8 | + * |
| 9 | + * special thanks to the Vue devtools for this solution |
| 10 | + */ |
| 11 | + |
| 12 | +export function installHook(window, devToolsVersion) { |
| 13 | + let listeners = {}; |
| 14 | + |
| 15 | + // XXX change how ApolloClient connects to the dev tools |
| 16 | + const hook = { |
| 17 | + ApolloClient: null, |
| 18 | + actionLog: [], |
| 19 | + devToolsVersion, |
| 20 | + on(event, fn) { |
| 21 | + event = "$" + event; |
| 22 | + (listeners[event] || (listeners[event] = [])).push(fn); |
| 23 | + }, |
| 24 | + once(event, fn) { |
| 25 | + const eventAlias = event; |
| 26 | + event = "$" + event; |
| 27 | + function on() { |
| 28 | + this.off(eventAlias, on); |
| 29 | + fn.apply(this, arguments); |
| 30 | + } |
| 31 | + (listeners[event] || (listeners[event] = [])).push(on); |
| 32 | + }, |
| 33 | + off(event, fn) { |
| 34 | + event = "$" + event; |
| 35 | + if (!arguments.length) { |
| 36 | + listeners = {}; |
| 37 | + } else { |
| 38 | + const cbs = listeners[event]; |
| 39 | + if (cbs) { |
| 40 | + if (!fn) { |
| 41 | + listeners[event] = null; |
| 42 | + } else { |
| 43 | + for (let i = 0, l = cbs.length; i < l; i++) { |
| 44 | + const cb = cbs[i]; |
| 45 | + if (cb === fn || cb.fn === fn) { |
| 46 | + cbs.splice(i, 1); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }, |
| 54 | + emit(event) { |
| 55 | + event = "$" + event; |
| 56 | + let cbs = listeners[event]; |
| 57 | + if (cbs) { |
| 58 | + const args = [].slice.call(arguments, 1); |
| 59 | + cbs = cbs.slice(); |
| 60 | + for (let i = 0, l = cbs.length; i < l; i++) { |
| 61 | + cbs[i].apply(this, args); |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + }; |
| 66 | + |
| 67 | + Object.defineProperty(window, "__APOLLO_DEVTOOLS_GLOBAL_HOOK__", { |
| 68 | + get() { |
| 69 | + return hook; |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + // XXX this is a patch to back support previous versions of Apollo Client |
| 74 | + // at somepoint we should remove this. |
| 75 | + // the newer version has the client connecting to the hook, not the other |
| 76 | + // way around that it currently does |
| 77 | + let interval; |
| 78 | + let count = 0; |
| 79 | + function findClient() { |
| 80 | + // only try for 10seconds |
| 81 | + if (count++ > 10) clearInterval(interval); |
| 82 | + if (!!window.__APOLLO_CLIENT__) { |
| 83 | + hook.ApolloClient = window.__APOLLO_CLIENT__; |
| 84 | + hook.ApolloClient.__actionHookForDevTools( |
| 85 | + ({ |
| 86 | + state: { queries, mutations }, |
| 87 | + dataWithOptimisticResults: inspector, |
| 88 | + }) => { |
| 89 | + hook.actionLog.push({ queries, mutations, inspector }); |
| 90 | + }, |
| 91 | + ); |
| 92 | + clearInterval(interval); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + interval = setInterval(findClient, 1000); |
| 97 | +} |
0 commit comments