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 {HookSourceAndMetadata} from './loadSourceAndMetadata';
    
  11. import type {HooksNode, HooksTree} from 'react-debug-tools/src/ReactDebugHooks';
    
  12. import type {HookNames} from 'react-devtools-shared/src/frontend/types';
    
  13. import type {FetchFileWithCaching} from 'react-devtools-shared/src/devtools/views/Components/FetchFileWithCachingContext';
    
  14. 
    
  15. import {withAsyncPerfMeasurements} from 'react-devtools-shared/src/PerformanceLoggingUtils';
    
  16. import WorkerizedParseSourceAndMetadata from './parseSourceAndMetadata.worker';
    
  17. import typeof * as ParseSourceAndMetadataModule from './parseSourceAndMetadata';
    
  18. import {flattenHooksList, loadSourceAndMetadata} from './loadSourceAndMetadata';
    
  19. 
    
  20. const workerizedParseHookNames: ParseSourceAndMetadataModule =
    
  21.   WorkerizedParseSourceAndMetadata();
    
  22. 
    
  23. export function parseSourceAndMetadata(
    
  24.   hooksList: Array<HooksNode>,
    
  25.   locationKeyToHookSourceAndMetadata: Map<string, HookSourceAndMetadata>,
    
  26. ): Promise<HookNames | null> {
    
  27.   return workerizedParseHookNames.parseSourceAndMetadata(
    
  28.     hooksList,
    
  29.     locationKeyToHookSourceAndMetadata,
    
  30.   );
    
  31. }
    
  32. 
    
  33. export const purgeCachedMetadata = workerizedParseHookNames.purgeCachedMetadata;
    
  34. 
    
  35. const EMPTY_MAP: HookNames = new Map();
    
  36. 
    
  37. export async function parseHookNames(
    
  38.   hooksTree: HooksTree,
    
  39.   fetchFileWithCaching: FetchFileWithCaching | null,
    
  40. ): Promise<HookNames | null> {
    
  41.   return withAsyncPerfMeasurements('parseHookNames', async () => {
    
  42.     const hooksList = flattenHooksList(hooksTree);
    
  43.     if (hooksList.length === 0) {
    
  44.       // This component tree contains no named hooks.
    
  45.       return EMPTY_MAP;
    
  46.     }
    
  47. 
    
  48.     // Runs on the main/UI thread so it can reuse Network cache:
    
  49.     const locationKeyToHookSourceAndMetadata = await loadSourceAndMetadata(
    
  50.       hooksList,
    
  51.       fetchFileWithCaching,
    
  52.     );
    
  53. 
    
  54.     // Runs in a Worker because it's CPU intensive:
    
  55.     return parseSourceAndMetadata(
    
  56.       hooksList,
    
  57.       locationKeyToHookSourceAndMetadata,
    
  58.     );
    
  59.   });
    
  60. }