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 Agent from './agent';
    
  11. 
    
  12. import {attach} from './renderer';
    
  13. import {attach as attachLegacy} from './legacy/renderer';
    
  14. import {hasAssignedBackend} from './utils';
    
  15. 
    
  16. import type {DevToolsHook, ReactRenderer, RendererInterface} from './types';
    
  17. 
    
  18. // this is the backend that is compatible with all older React versions
    
  19. function isMatchingRender(version: string): boolean {
    
  20.   return !hasAssignedBackend(version);
    
  21. }
    
  22. 
    
  23. export type InitBackend = typeof initBackend;
    
  24. 
    
  25. export function initBackend(
    
  26.   hook: DevToolsHook,
    
  27.   agent: Agent,
    
  28.   global: Object,
    
  29. ): () => void {
    
  30.   if (hook == null) {
    
  31.     // DevTools didn't get injected into this page (maybe b'c of the contentType).
    
  32.     return () => {};
    
  33.   }
    
  34. 
    
  35.   const subs = [
    
  36.     hook.sub(
    
  37.       'renderer-attached',
    
  38.       ({
    
  39.         id,
    
  40.         renderer,
    
  41.         rendererInterface,
    
  42.       }: {
    
  43.         id: number,
    
  44.         renderer: ReactRenderer,
    
  45.         rendererInterface: RendererInterface,
    
  46.         ...
    
  47.       }) => {
    
  48.         agent.setRendererInterface(id, rendererInterface);
    
  49. 
    
  50.         // Now that the Store and the renderer interface are connected,
    
  51.         // it's time to flush the pending operation codes to the frontend.
    
  52.         rendererInterface.flushInitialOperations();
    
  53.       },
    
  54.     ),
    
  55. 
    
  56.     hook.sub('unsupported-renderer-version', (id: number) => {
    
  57.       agent.onUnsupportedRenderer(id);
    
  58.     }),
    
  59. 
    
  60.     hook.sub('fastRefreshScheduled', agent.onFastRefreshScheduled),
    
  61.     hook.sub('operations', agent.onHookOperations),
    
  62.     hook.sub('traceUpdates', agent.onTraceUpdates),
    
  63. 
    
  64.     // TODO Add additional subscriptions required for profiling mode
    
  65.   ];
    
  66. 
    
  67.   const attachRenderer = (id: number, renderer: ReactRenderer) => {
    
  68.     // only attach if the renderer is compatible with the current version of the backend
    
  69.     if (!isMatchingRender(renderer.reconcilerVersion || renderer.version)) {
    
  70.       return;
    
  71.     }
    
  72.     let rendererInterface = hook.rendererInterfaces.get(id);
    
  73. 
    
  74.     // Inject any not-yet-injected renderers (if we didn't reload-and-profile)
    
  75.     if (rendererInterface == null) {
    
  76.       if (typeof renderer.findFiberByHostInstance === 'function') {
    
  77.         // react-reconciler v16+
    
  78.         rendererInterface = attach(hook, id, renderer, global);
    
  79.       } else if (renderer.ComponentTree) {
    
  80.         // react-dom v15
    
  81.         rendererInterface = attachLegacy(hook, id, renderer, global);
    
  82.       } else {
    
  83.         // Older react-dom or other unsupported renderer version
    
  84.       }
    
  85. 
    
  86.       if (rendererInterface != null) {
    
  87.         hook.rendererInterfaces.set(id, rendererInterface);
    
  88.       }
    
  89.     }
    
  90. 
    
  91.     // Notify the DevTools frontend about new renderers.
    
  92.     // This includes any that were attached early (via __REACT_DEVTOOLS_ATTACH__).
    
  93.     if (rendererInterface != null) {
    
  94.       hook.emit('renderer-attached', {
    
  95.         id,
    
  96.         renderer,
    
  97.         rendererInterface,
    
  98.       });
    
  99.     } else {
    
  100.       hook.emit('unsupported-renderer-version', id);
    
  101.     }
    
  102.   };
    
  103. 
    
  104.   // Connect renderers that have already injected themselves.
    
  105.   hook.renderers.forEach((renderer, id) => {
    
  106.     attachRenderer(id, renderer);
    
  107.   });
    
  108. 
    
  109.   // Connect any new renderers that injected themselves.
    
  110.   subs.push(
    
  111.     hook.sub(
    
  112.       'renderer',
    
  113.       ({id, renderer}: {id: number, renderer: ReactRenderer, ...}) => {
    
  114.         attachRenderer(id, renderer);
    
  115.       },
    
  116.     ),
    
  117.   );
    
  118. 
    
  119.   hook.emit('react-devtools', agent);
    
  120.   hook.reactDevtoolsAgent = agent;
    
  121.   const onAgentShutdown = () => {
    
  122.     subs.forEach(fn => fn());
    
  123.     hook.rendererInterfaces.forEach(rendererInterface => {
    
  124.       rendererInterface.cleanup();
    
  125.     });
    
  126.     hook.reactDevtoolsAgent = null;
    
  127.   };
    
  128.   agent.addListener('shutdown', onAgentShutdown);
    
  129.   subs.push(() => {
    
  130.     agent.removeListener('shutdown', onAgentShutdown);
    
  131.   });
    
  132. 
    
  133.   return () => {
    
  134.     subs.forEach(fn => fn());
    
  135.   };
    
  136. }