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 strict-local
    
  8.  */
    
  9. 
    
  10. import {enableLogger} from 'react-devtools-feature-flags';
    
  11. 
    
  12. export type LoggerEvent =
    
  13.   | {
    
  14.       +event_name: 'loaded-dev-tools',
    
  15.     }
    
  16.   | {
    
  17.       +event_name: 'error',
    
  18.       +error_message: string | null,
    
  19.       +error_stack: string | null,
    
  20.       +error_component_stack: string | null,
    
  21.     }
    
  22.   | {
    
  23.       +event_name: 'selected-components-tab',
    
  24.     }
    
  25.   | {
    
  26.       +event_name: 'selected-profiler-tab',
    
  27.     }
    
  28.   | {
    
  29.       +event_name: 'load-hook-names',
    
  30.       +event_status: 'success' | 'error' | 'timeout' | 'unknown',
    
  31.       +duration_ms: number,
    
  32.       +inspected_element_display_name: string | null,
    
  33.       +inspected_element_number_of_hooks: number | null,
    
  34.     }
    
  35.   | {
    
  36.       +event_name: 'select-element',
    
  37.       +metadata: {
    
  38.         +source: string,
    
  39.       },
    
  40.     }
    
  41.   | {
    
  42.       +event_name: 'inspect-element-button-clicked',
    
  43.     }
    
  44.   | {
    
  45.       +event_name: 'profiling-start',
    
  46.       +metadata: {
    
  47.         +current_tab: string,
    
  48.       },
    
  49.     }
    
  50.   | {
    
  51.       +event_name: 'profiler-tab-changed',
    
  52.       +metadata: {
    
  53.         +tabId: string,
    
  54.       },
    
  55.     }
    
  56.   | {
    
  57.       +event_name: 'settings-changed',
    
  58.       +metadata: {
    
  59.         +key: string,
    
  60.         +value: any,
    
  61.         ...
    
  62.       },
    
  63.     };
    
  64. 
    
  65. export type LogFunction = LoggerEvent => void | Promise<void>;
    
  66. 
    
  67. let logFunctions: Array<LogFunction> = [];
    
  68. export const logEvent: LogFunction =
    
  69.   enableLogger === true
    
  70.     ? function logEvent(event: LoggerEvent): void {
    
  71.         logFunctions.forEach(log => {
    
  72.           log(event);
    
  73.         });
    
  74.       }
    
  75.     : function logEvent() {};
    
  76. 
    
  77. export const registerEventLogger: (logFunction: LogFunction) => () => void =
    
  78.   enableLogger === true
    
  79.     ? function registerEventLogger(logFunction: LogFunction): () => void {
    
  80.         if (enableLogger) {
    
  81.           logFunctions.push(logFunction);
    
  82.           return function unregisterEventLogger() {
    
  83.             logFunctions = logFunctions.filter(log => log !== logFunction);
    
  84.           };
    
  85.         }
    
  86.         return () => {};
    
  87.       }
    
  88.     : function registerEventLogger(logFunction: LogFunction) {
    
  89.         return () => {};
    
  90.       };