| 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 | import type { |
| 11 | ReactRenderer, |
| 12 | RendererInterface, |
| 13 | DevToolsHook, |
| 14 | RendererID, |
| 15 | ProfilingSettings, |
| 16 | } from 'react-devtools-shared/src/backend/types'; |
| 17 | import type {ComponentFilter} from 'react-devtools-shared/src/frontend/types'; |
| 18 | |
| 19 | import {attach as attachFlight} from 'react-devtools-shared/src/backend/flight/renderer'; |
| 20 | import {attach as attachFiber} from 'react-devtools-shared/src/backend/fiber/renderer'; |
| 21 | import {attach as attachLegacy} from 'react-devtools-shared/src/backend/legacy/renderer'; |
| 22 | import {hasAssignedBackend} from 'react-devtools-shared/src/backend/utils'; |
| 23 | |
| 24 | // this is the backend that is compatible with all older React versions |
| 25 | function isMatchingRender(version: string): boolean { |
| 26 | return !hasAssignedBackend(version); |
| 27 | } |
| 28 | |
| 29 | export default function attachRenderer( |
| 30 | hook: DevToolsHook, |
| 31 | id: RendererID, |
| 32 | renderer: ReactRenderer, |
| 33 | global: Object, |
| 34 | shouldStartProfilingNow: boolean, |
| 35 | profilingSettings: ProfilingSettings, |
| 36 | componentFiltersOrComponentFiltersPromise: |
| 37 | | Array<ComponentFilter> |
| 38 | | Promise<Array<ComponentFilter>>, |
| 39 | ): RendererInterface | void { |
| 40 | // only attach if the renderer is compatible with the current version of the backend |
| 41 | if (!isMatchingRender(renderer.reconcilerVersion || renderer.version)) { |
| 42 | return; |
| 43 | } |
| 44 | let rendererInterface = hook.rendererInterfaces.get(id); |
| 45 | |
| 46 | // Inject any not-yet-injected renderers (if we didn't reload-and-profile) |
| 47 | if (rendererInterface == null) { |
| 48 | if (typeof renderer.getCurrentComponentInfo === 'function') { |
| 49 | // react-flight/client |
| 50 | rendererInterface = attachFlight(hook, id, renderer, global); |
| 51 | } else if ( |
| 52 | // v16-19 |
| 53 | typeof renderer.findFiberByHostInstance === 'function' || |
| 54 | // v16.8+ |
| 55 | renderer.currentDispatcherRef != null |
| 56 | ) { |
| 57 | // react-reconciler v16+ |
| 58 | rendererInterface = attachFiber( |
| 59 | hook, |
| 60 | id, |
| 61 | renderer, |
| 62 | global, |
| 63 | shouldStartProfilingNow, |
| 64 | profilingSettings, |
| 65 | componentFiltersOrComponentFiltersPromise, |
| 66 | ); |
| 67 | } else if (renderer.ComponentTree) { |
| 68 | // react-dom v15 |
| 69 | rendererInterface = attachLegacy(hook, id, renderer, global); |
| 70 | } else { |
| 71 | // Older react-dom or other unsupported renderer version |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return rendererInterface; |
| 76 | } |