(options: ?ConnectOptions)
| 76 | } |
| 77 | |
| 78 | export function connectToDevTools(options: ?ConnectOptions) { |
| 79 | const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__; |
| 80 | if (hook == null) { |
| 81 | // DevTools didn't get injected into this page (maybe b'c of the contentType). |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const { |
| 86 | host = 'localhost', |
| 87 | nativeStyleEditorValidAttributes, |
| 88 | useHttps = false, |
| 89 | port = 8097, |
| 90 | websocket, |
| 91 | resolveRNStyle = (null: $FlowFixMe), |
| 92 | retryConnectionDelay = 2000, |
| 93 | isAppActive = () => true, |
| 94 | onSettingsUpdated, |
| 95 | isReloadAndProfileSupported = getIsReloadAndProfileSupported(), |
| 96 | isProfiling, |
| 97 | onReloadAndProfile, |
| 98 | onReloadAndProfileFlagsReset, |
| 99 | } = options || {}; |
| 100 | |
| 101 | const protocol = useHttps ? 'wss' : 'ws'; |
| 102 | let retryTimeoutID: TimeoutID | null = null; |
| 103 | |
| 104 | function scheduleRetry() { |
| 105 | if (retryTimeoutID === null) { |
| 106 | // Two seconds because RN had issues with quick retries. |
| 107 | retryTimeoutID = setTimeout( |
| 108 | () => connectToDevTools(options), |
| 109 | retryConnectionDelay, |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (!isAppActive()) { |
| 115 | // If the app is in background, maybe retry later. |
| 116 | // Don't actually attempt to connect until we're in foreground. |
| 117 | scheduleRetry(); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | let bridge: BackendBridge | null = null; |
| 122 | |
| 123 | const messageListeners = []; |
| 124 | const uri = protocol + '://' + host + ':' + port; |
| 125 | |
| 126 | // If existing websocket is passed, use it. |
| 127 | // This is necessary to support our custom integrations. |
| 128 | // See D6251744. |
| 129 | const ws = websocket ? websocket : new window.WebSocket(uri); |
| 130 | ws.onclose = handleClose; |
| 131 | ws.onerror = handleFailed; |
| 132 | ws.onmessage = handleMessage; |
| 133 | ws.onopen = function () { |
| 134 | bridge = new Bridge({ |
| 135 | listen(fn) { |
no test coverage detected