| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | declare const __REACT_DEVTOOLS_GLOBAL_HOOK__: Object | void; |
| 11 | |
| 12 | export function injectInternals(internals: Object): boolean { |
| 13 | if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') { |
| 14 | // No DevTools |
| 15 | return false; |
| 16 | } |
| 17 | const hook = __REACT_DEVTOOLS_GLOBAL_HOOK__; |
| 18 | if (hook.isDisabled) { |
| 19 | // This isn't a real property on the hook, but it can be set to opt out |
| 20 | // of DevTools integration and associated warnings and logs. |
| 21 | // https://github.com/facebook/react/issues/3877 |
| 22 | return true; |
| 23 | } |
| 24 | if (!hook.supportsFlight) { |
| 25 | // DevTools exists, even though it doesn't support Flight. |
| 26 | return true; |
| 27 | } |
| 28 | try { |
| 29 | hook.inject(internals); |
| 30 | } catch (err) { |
| 31 | // Catch all errors because it is unsafe to throw during initialization. |
| 32 | if (__DEV__) { |
| 33 | console.error('React instrumentation encountered an error: %o.', err); |
| 34 | } |
| 35 | } |
| 36 | if (hook.checkDCE) { |
| 37 | // This is the real DevTools. |
| 38 | return true; |
| 39 | } else { |
| 40 | // This is likely a hook installed by Fast Refresh runtime. |
| 41 | return false; |
| 42 | } |
| 43 | } |