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 {
    
  11.   importFromChromeTimeline,
    
  12.   Flamechart as SpeedscopeFlamechart,
    
  13. } from '@elg/speedscope';
    
  14. import type {TimelineEvent} from '@elg/speedscope';
    
  15. import type {
    
  16.   ErrorStackFrame,
    
  17.   BatchUID,
    
  18.   Flamechart,
    
  19.   Milliseconds,
    
  20.   NativeEvent,
    
  21.   NetworkMeasure,
    
  22.   Phase,
    
  23.   ReactLane,
    
  24.   ReactComponentMeasure,
    
  25.   ReactComponentMeasureType,
    
  26.   ReactMeasure,
    
  27.   ReactMeasureType,
    
  28.   TimelineData,
    
  29.   SchedulingEvent,
    
  30.   SuspenseEvent,
    
  31. } from '../types';
    
  32. import {
    
  33.   REACT_TOTAL_NUM_LANES,
    
  34.   SCHEDULING_PROFILER_VERSION,
    
  35.   SNAPSHOT_MAX_HEIGHT,
    
  36. } from '../constants';
    
  37. import InvalidProfileError from './InvalidProfileError';
    
  38. import {getBatchRange} from '../utils/getBatchRange';
    
  39. import ErrorStackParser from 'error-stack-parser';
    
  40. 
    
  41. type MeasureStackElement = {
    
  42.   type: ReactMeasureType,
    
  43.   depth: number,
    
  44.   measure: ReactMeasure,
    
  45.   startTime: Milliseconds,
    
  46.   stopTime?: Milliseconds,
    
  47. };
    
  48. 
    
  49. type ProcessorState = {
    
  50.   asyncProcessingPromises: Promise<any>[],
    
  51.   batchUID: BatchUID,
    
  52.   currentReactComponentMeasure: ReactComponentMeasure | null,
    
  53.   internalModuleCurrentStackFrame: ErrorStackFrame | null,
    
  54.   internalModuleStackStringSet: Set<string>,
    
  55.   measureStack: MeasureStackElement[],
    
  56.   nativeEventStack: NativeEvent[],
    
  57.   nextRenderShouldGenerateNewBatchID: boolean,
    
  58.   potentialLongEvents: Array<[NativeEvent, BatchUID]>,
    
  59.   potentialLongNestedUpdate: SchedulingEvent | null,
    
  60.   potentialLongNestedUpdates: Array<[SchedulingEvent, BatchUID]>,
    
  61.   potentialSuspenseEventsOutsideOfTransition: Array<
    
  62.     [SuspenseEvent, ReactLane[]],
    
  63.   >,
    
  64.   requestIdToNetworkMeasureMap: Map<string, NetworkMeasure>,
    
  65.   uidCounter: BatchUID,
    
  66.   unresolvedSuspenseEvents: Map<string, SuspenseEvent>,
    
  67. };
    
  68. 
    
  69. const NATIVE_EVENT_DURATION_THRESHOLD = 20;
    
  70. const NESTED_UPDATE_DURATION_THRESHOLD = 20;
    
  71. 
    
  72. const WARNING_STRINGS = {
    
  73.   LONG_EVENT_HANDLER:
    
  74.     'An event handler scheduled a big update with React. Consider using the Transition API to defer some of this work.',
    
  75.   NESTED_UPDATE:
    
  76.     'A big nested update was scheduled during layout. ' +
    
  77.     'Nested updates require React to re-render synchronously before the browser can paint. ' +
    
  78.     'Consider delaying this update by moving it to a passive effect (useEffect).',
    
  79.   SUSPEND_DURING_UPDATE:
    
  80.     'A component suspended during an update which caused a fallback to be shown. ' +
    
  81.     "Consider using the Transition API to avoid hiding components after they've been mounted.",
    
  82. };
    
  83. 
    
  84. // Exported for tests
    
  85. export function getLanesFromTransportDecimalBitmask(
    
  86.   laneBitmaskString: string,
    
  87. ): ReactLane[] {
    
  88.   const laneBitmask = parseInt(laneBitmaskString, 10);
    
  89. 
    
  90.   // As negative numbers are stored in two's complement format, our bitmask
    
  91.   // checks will be thrown off by them.
    
  92.   if (laneBitmask < 0) {
    
  93.     return [];
    
  94.   }
    
  95. 
    
  96.   const lanes = [];
    
  97.   let powersOfTwo = 0;
    
  98.   while (powersOfTwo <= REACT_TOTAL_NUM_LANES) {
    
  99.     if ((1 << powersOfTwo) & laneBitmask) {
    
  100.       lanes.push(powersOfTwo);
    
  101.     }
    
  102.     powersOfTwo++;
    
  103.   }
    
  104.   return lanes;
    
  105. }
    
  106. 
    
  107. function updateLaneToLabelMap(
    
  108.   profilerData: TimelineData,
    
  109.   laneLabelTuplesString: string,
    
  110. ): void {
    
  111.   // These marks appear multiple times in the data;
    
  112.   // We only need to extact them once.
    
  113.   if (profilerData.laneToLabelMap.size === 0) {
    
  114.     const laneLabelTuples = laneLabelTuplesString.split(',');
    
  115.     for (let laneIndex = 0; laneIndex < laneLabelTuples.length; laneIndex++) {
    
  116.       // The numeric lane value (e.g. 64) isn't important.
    
  117.       // The profiler parses and stores the lane's position within the bitmap,
    
  118.       // (e.g. lane 1 is index 0, lane 16 is index 4).
    
  119.       profilerData.laneToLabelMap.set(laneIndex, laneLabelTuples[laneIndex]);
    
  120.     }
    
  121.   }
    
  122. }
    
  123. 
    
  124. let profilerVersion = null;
    
  125. 
    
  126. function getLastType(stack: $PropertyType<ProcessorState, 'measureStack'>) {
    
  127.   if (stack.length > 0) {
    
  128.     const {type} = stack[stack.length - 1];
    
  129.     return type;
    
  130.   }
    
  131.   return null;
    
  132. }
    
  133. 
    
  134. function getDepth(stack: $PropertyType<ProcessorState, 'measureStack'>) {
    
  135.   if (stack.length > 0) {
    
  136.     const {depth, type} = stack[stack.length - 1];
    
  137.     return type === 'render-idle' ? depth : depth + 1;
    
  138.   }
    
  139.   return 0;
    
  140. }
    
  141. 
    
  142. function markWorkStarted(
    
  143.   type: ReactMeasureType,
    
  144.   startTime: Milliseconds,
    
  145.   lanes: ReactLane[],
    
  146.   currentProfilerData: TimelineData,
    
  147.   state: ProcessorState,
    
  148. ) {
    
  149.   const {batchUID, measureStack} = state;
    
  150.   const depth = getDepth(measureStack);
    
  151. 
    
  152.   const measure: ReactMeasure = {
    
  153.     type,
    
  154.     batchUID,
    
  155.     depth,
    
  156.     lanes,
    
  157.     timestamp: startTime,
    
  158.     duration: 0,
    
  159.   };
    
  160. 
    
  161.   state.measureStack.push({depth, measure, startTime, type});
    
  162. 
    
  163.   // This array is pre-initialized when the batchUID is generated.
    
  164.   const measures = currentProfilerData.batchUIDToMeasuresMap.get(batchUID);
    
  165.   if (measures != null) {
    
  166.     measures.push(measure);
    
  167.   } else {
    
  168.     currentProfilerData.batchUIDToMeasuresMap.set(state.batchUID, [measure]);
    
  169.   }
    
  170. 
    
  171.   // This array is pre-initialized before processing starts.
    
  172.   lanes.forEach(lane => {
    
  173.     ((currentProfilerData.laneToReactMeasureMap.get(
    
  174.       lane,
    
  175.     ): any): ReactMeasure[]).push(measure);
    
  176.   });
    
  177. }
    
  178. 
    
  179. function markWorkCompleted(
    
  180.   type: ReactMeasureType,
    
  181.   stopTime: Milliseconds,
    
  182.   currentProfilerData: TimelineData,
    
  183.   stack: $PropertyType<ProcessorState, 'measureStack'>,
    
  184. ) {
    
  185.   if (stack.length === 0) {
    
  186.     console.error(
    
  187.       'Unexpected type "%s" completed at %sms while stack is empty.',
    
  188.       type,
    
  189.       stopTime,
    
  190.     );
    
  191.     // Ignore work "completion" user timing mark that doesn't complete anything
    
  192.     return;
    
  193.   }
    
  194. 
    
  195.   const last = stack[stack.length - 1];
    
  196.   if (last.type !== type) {
    
  197.     console.error(
    
  198.       'Unexpected type "%s" completed at %sms before "%s" completed.',
    
  199.       type,
    
  200.       stopTime,
    
  201.       last.type,
    
  202.     );
    
  203.   }
    
  204. 
    
  205.   const {measure, startTime} = stack.pop();
    
  206.   if (!measure) {
    
  207.     console.error('Could not find matching measure for type "%s".', type);
    
  208.   }
    
  209. 
    
  210.   // $FlowFixMe[cannot-write] This property should not be writable outside of this function.
    
  211.   measure.duration = stopTime - startTime;
    
  212. }
    
  213. 
    
  214. function throwIfIncomplete(
    
  215.   type: ReactMeasureType,
    
  216.   stack: $PropertyType<ProcessorState, 'measureStack'>,
    
  217. ) {
    
  218.   const lastIndex = stack.length - 1;
    
  219.   if (lastIndex >= 0) {
    
  220.     const last = stack[lastIndex];
    
  221.     if (last.stopTime === undefined && last.type === type) {
    
  222.       throw new InvalidProfileError(
    
  223.         `Unexpected type "${type}" started before "${last.type}" completed.`,
    
  224.       );
    
  225.     }
    
  226.   }
    
  227. }
    
  228. 
    
  229. function processEventDispatch(
    
  230.   event: TimelineEvent,
    
  231.   timestamp: Milliseconds,
    
  232.   profilerData: TimelineData,
    
  233.   state: ProcessorState,
    
  234. ) {
    
  235.   const data = event.args.data;
    
  236.   const type = data.type;
    
  237. 
    
  238.   if (type.startsWith('react-')) {
    
  239.     const stackTrace = data.stackTrace;
    
  240.     if (stackTrace) {
    
  241.       const topFrame = stackTrace[stackTrace.length - 1];
    
  242.       if (topFrame.url.includes('/react-dom.')) {
    
  243.         // Filter out fake React events dispatched by invokeGuardedCallbackDev.
    
  244.         return;
    
  245.       }
    
  246.     }
    
  247.   }
    
  248. 
    
  249.   // Reduce noise from events like DOMActivate, load/unload, etc. which are usually not relevant
    
  250.   if (
    
  251.     type === 'blur' ||
    
  252.     type === 'click' ||
    
  253.     type === 'input' ||
    
  254.     type.startsWith('focus') ||
    
  255.     type.startsWith('key') ||
    
  256.     type.startsWith('mouse') ||
    
  257.     type.startsWith('pointer')
    
  258.   ) {
    
  259.     const duration = event.dur / 1000;
    
  260. 
    
  261.     let depth = 0;
    
  262. 
    
  263.     while (state.nativeEventStack.length > 0) {
    
  264.       const prevNativeEvent =
    
  265.         state.nativeEventStack[state.nativeEventStack.length - 1];
    
  266.       const prevStopTime = prevNativeEvent.timestamp + prevNativeEvent.duration;
    
  267. 
    
  268.       if (timestamp < prevStopTime) {
    
  269.         depth = prevNativeEvent.depth + 1;
    
  270.         break;
    
  271.       } else {
    
  272.         state.nativeEventStack.pop();
    
  273.       }
    
  274.     }
    
  275. 
    
  276.     const nativeEvent = {
    
  277.       depth,
    
  278.       duration,
    
  279.       timestamp,
    
  280.       type,
    
  281.       warning: null,
    
  282.     };
    
  283. 
    
  284.     profilerData.nativeEvents.push(nativeEvent);
    
  285. 
    
  286.     // Keep track of curent event in case future ones overlap.
    
  287.     // We separate them into different vertical lanes in this case.
    
  288.     state.nativeEventStack.push(nativeEvent);
    
  289.   }
    
  290. }
    
  291. 
    
  292. function processResourceFinish(
    
  293.   event: TimelineEvent,
    
  294.   timestamp: Milliseconds,
    
  295.   profilerData: TimelineData,
    
  296.   state: ProcessorState,
    
  297. ) {
    
  298.   const requestId = event.args.data.requestId;
    
  299.   const networkMeasure = state.requestIdToNetworkMeasureMap.get(requestId);
    
  300.   if (networkMeasure != null) {
    
  301.     networkMeasure.finishTimestamp = timestamp;
    
  302.     if (networkMeasure.firstReceivedDataTimestamp === 0) {
    
  303.       networkMeasure.firstReceivedDataTimestamp = timestamp;
    
  304.     }
    
  305.     if (networkMeasure.lastReceivedDataTimestamp === 0) {
    
  306.       networkMeasure.lastReceivedDataTimestamp = timestamp;
    
  307.     }
    
  308. 
    
  309.     // Clean up now that the resource is done.
    
  310.     state.requestIdToNetworkMeasureMap.delete(event.args.data.requestId);
    
  311.   }
    
  312. }
    
  313. 
    
  314. function processResourceReceivedData(
    
  315.   event: TimelineEvent,
    
  316.   timestamp: Milliseconds,
    
  317.   profilerData: TimelineData,
    
  318.   state: ProcessorState,
    
  319. ) {
    
  320.   const requestId = event.args.data.requestId;
    
  321.   const networkMeasure = state.requestIdToNetworkMeasureMap.get(requestId);
    
  322.   if (networkMeasure != null) {
    
  323.     if (networkMeasure.firstReceivedDataTimestamp === 0) {
    
  324.       networkMeasure.firstReceivedDataTimestamp = timestamp;
    
  325.     }
    
  326.     networkMeasure.lastReceivedDataTimestamp = timestamp;
    
  327.     networkMeasure.finishTimestamp = timestamp;
    
  328.   }
    
  329. }
    
  330. 
    
  331. function processResourceReceiveResponse(
    
  332.   event: TimelineEvent,
    
  333.   timestamp: Milliseconds,
    
  334.   profilerData: TimelineData,
    
  335.   state: ProcessorState,
    
  336. ) {
    
  337.   const requestId = event.args.data.requestId;
    
  338.   const networkMeasure = state.requestIdToNetworkMeasureMap.get(requestId);
    
  339.   if (networkMeasure != null) {
    
  340.     networkMeasure.receiveResponseTimestamp = timestamp;
    
  341.   }
    
  342. }
    
  343. 
    
  344. function processScreenshot(
    
  345.   event: TimelineEvent,
    
  346.   timestamp: Milliseconds,
    
  347.   profilerData: TimelineData,
    
  348.   state: ProcessorState,
    
  349. ) {
    
  350.   const encodedSnapshot = event.args.snapshot; // Base 64 encoded
    
  351. 
    
  352.   const snapshot = {
    
  353.     height: 0,
    
  354.     image: null,
    
  355.     imageSource: `data:image/png;base64,${encodedSnapshot}`,
    
  356.     timestamp,
    
  357.     width: 0,
    
  358.   };
    
  359. 
    
  360.   // Delay processing until we've extracted snapshot dimensions.
    
  361.   let resolveFn = ((null: any): Function);
    
  362.   state.asyncProcessingPromises.push(
    
  363.     new Promise(resolve => {
    
  364.       resolveFn = resolve;
    
  365.     }),
    
  366.   );
    
  367. 
    
  368.   // Parse the Base64 image data to determine native size.
    
  369.   // This will be used later to scale for display within the thumbnail strip.
    
  370.   fetch(snapshot.imageSource)
    
  371.     .then(response => response.blob())
    
  372.     .then(blob => {
    
  373.       // $FlowFixMe[cannot-resolve-name] createImageBitmap
    
  374.       createImageBitmap(blob).then(bitmap => {
    
  375.         snapshot.height = bitmap.height;
    
  376.         snapshot.width = bitmap.width;
    
  377. 
    
  378.         resolveFn();
    
  379.       });
    
  380.     });
    
  381. 
    
  382.   profilerData.snapshots.push(snapshot);
    
  383. }
    
  384. 
    
  385. function processResourceSendRequest(
    
  386.   event: TimelineEvent,
    
  387.   timestamp: Milliseconds,
    
  388.   profilerData: TimelineData,
    
  389.   state: ProcessorState,
    
  390. ) {
    
  391.   const data = event.args.data;
    
  392.   const requestId = data.requestId;
    
  393. 
    
  394.   const availableDepths = new Array<boolean>(
    
  395.     state.requestIdToNetworkMeasureMap.size + 1,
    
  396.   ).fill(true);
    
  397.   state.requestIdToNetworkMeasureMap.forEach(({depth}) => {
    
  398.     availableDepths[depth] = false;
    
  399.   });
    
  400. 
    
  401.   let depth = 0;
    
  402.   for (let i = 0; i < availableDepths.length; i++) {
    
  403.     if (availableDepths[i]) {
    
  404.       depth = i;
    
  405.       break;
    
  406.     }
    
  407.   }
    
  408. 
    
  409.   const networkMeasure: NetworkMeasure = {
    
  410.     depth,
    
  411.     finishTimestamp: 0,
    
  412.     firstReceivedDataTimestamp: 0,
    
  413.     lastReceivedDataTimestamp: 0,
    
  414.     requestId,
    
  415.     requestMethod: data.requestMethod,
    
  416.     priority: data.priority,
    
  417.     sendRequestTimestamp: timestamp,
    
  418.     receiveResponseTimestamp: 0,
    
  419.     url: data.url,
    
  420.   };
    
  421. 
    
  422.   state.requestIdToNetworkMeasureMap.set(requestId, networkMeasure);
    
  423. 
    
  424.   profilerData.networkMeasures.push(networkMeasure);
    
  425.   networkMeasure.sendRequestTimestamp = timestamp;
    
  426. }
    
  427. 
    
  428. function processTimelineEvent(
    
  429.   event: TimelineEvent,
    
  430.   /** Finalized profiler data up to `event`. May be mutated. */
    
  431.   currentProfilerData: TimelineData,
    
  432.   /** Intermediate processor state. May be mutated. */
    
  433.   state: ProcessorState,
    
  434. ) {
    
  435.   const {cat, name, ts, ph} = event;
    
  436. 
    
  437.   const startTime = (ts - currentProfilerData.startTime) / 1000;
    
  438. 
    
  439.   switch (cat) {
    
  440.     case 'disabled-by-default-devtools.screenshot':
    
  441.       processScreenshot(event, startTime, currentProfilerData, state);
    
  442.       break;
    
  443.     case 'devtools.timeline':
    
  444.       switch (name) {
    
  445.         case 'EventDispatch':
    
  446.           processEventDispatch(event, startTime, currentProfilerData, state);
    
  447.           break;
    
  448.         case 'ResourceFinish':
    
  449.           processResourceFinish(event, startTime, currentProfilerData, state);
    
  450.           break;
    
  451.         case 'ResourceReceivedData':
    
  452.           processResourceReceivedData(
    
  453.             event,
    
  454.             startTime,
    
  455.             currentProfilerData,
    
  456.             state,
    
  457.           );
    
  458.           break;
    
  459.         case 'ResourceReceiveResponse':
    
  460.           processResourceReceiveResponse(
    
  461.             event,
    
  462.             startTime,
    
  463.             currentProfilerData,
    
  464.             state,
    
  465.           );
    
  466.           break;
    
  467.         case 'ResourceSendRequest':
    
  468.           processResourceSendRequest(
    
  469.             event,
    
  470.             startTime,
    
  471.             currentProfilerData,
    
  472.             state,
    
  473.           );
    
  474.           break;
    
  475.       }
    
  476.       break;
    
  477.     case 'blink.user_timing':
    
  478.       if (name.startsWith('--react-version-')) {
    
  479.         const [reactVersion] = name.slice(16).split('-');
    
  480.         currentProfilerData.reactVersion = reactVersion;
    
  481.       } else if (name.startsWith('--profiler-version-')) {
    
  482.         const [versionString] = name.slice(19).split('-');
    
  483.         profilerVersion = parseInt(versionString, 10);
    
  484.         if (profilerVersion !== SCHEDULING_PROFILER_VERSION) {
    
  485.           throw new InvalidProfileError(
    
  486.             `This version of profiling data (${versionString}) is not supported by the current profiler.`,
    
  487.           );
    
  488.         }
    
  489.       } else if (name.startsWith('--react-lane-labels-')) {
    
  490.         const [laneLabelTuplesString] = name.slice(20).split('-');
    
  491.         updateLaneToLabelMap(currentProfilerData, laneLabelTuplesString);
    
  492.       } else if (name.startsWith('--component-')) {
    
  493.         processReactComponentMeasure(
    
  494.           name,
    
  495.           startTime,
    
  496.           currentProfilerData,
    
  497.           state,
    
  498.         );
    
  499.       } else if (name.startsWith('--schedule-render-')) {
    
  500.         const [laneBitmaskString] = name.slice(18).split('-');
    
  501. 
    
  502.         currentProfilerData.schedulingEvents.push({
    
  503.           type: 'schedule-render',
    
  504.           lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  505.           timestamp: startTime,
    
  506.           warning: null,
    
  507.         });
    
  508.       } else if (name.startsWith('--schedule-forced-update-')) {
    
  509.         const [laneBitmaskString, componentName] = name.slice(25).split('-');
    
  510. 
    
  511.         const forceUpdateEvent = {
    
  512.           type: 'schedule-force-update',
    
  513.           lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  514.           componentName,
    
  515.           timestamp: startTime,
    
  516.           warning: null,
    
  517.         };
    
  518. 
    
  519.         // If this is a nested update, make a note of it.
    
  520.         // Once we're done processing events, we'll check to see if it was a long update and warn about it.
    
  521.         if (state.measureStack.find(({type}) => type === 'commit')) {
    
  522.           state.potentialLongNestedUpdate = forceUpdateEvent;
    
  523.         }
    
  524. 
    
  525.         currentProfilerData.schedulingEvents.push(forceUpdateEvent);
    
  526.       } else if (name.startsWith('--schedule-state-update-')) {
    
  527.         const [laneBitmaskString, componentName] = name.slice(24).split('-');
    
  528. 
    
  529.         const stateUpdateEvent = {
    
  530.           type: 'schedule-state-update',
    
  531.           lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  532.           componentName,
    
  533.           timestamp: startTime,
    
  534.           warning: null,
    
  535.         };
    
  536. 
    
  537.         // If this is a nested update, make a note of it.
    
  538.         // Once we're done processing events, we'll check to see if it was a long update and warn about it.
    
  539.         if (state.measureStack.find(({type}) => type === 'commit')) {
    
  540.           state.potentialLongNestedUpdate = stateUpdateEvent;
    
  541.         }
    
  542. 
    
  543.         currentProfilerData.schedulingEvents.push(stateUpdateEvent);
    
  544.       } else if (name.startsWith('--error-')) {
    
  545.         const [componentName, phase, message] = name.slice(8).split('-');
    
  546. 
    
  547.         currentProfilerData.thrownErrors.push({
    
  548.           componentName,
    
  549.           message,
    
  550.           phase: ((phase: any): Phase),
    
  551.           timestamp: startTime,
    
  552.           type: 'thrown-error',
    
  553.         });
    
  554.       } else if (name.startsWith('--suspense-suspend-')) {
    
  555.         const [id, componentName, phase, laneBitmaskString, promiseName] = name
    
  556.           .slice(19)
    
  557.           .split('-');
    
  558.         const lanes = getLanesFromTransportDecimalBitmask(laneBitmaskString);
    
  559. 
    
  560.         const availableDepths = new Array<boolean>(
    
  561.           state.unresolvedSuspenseEvents.size + 1,
    
  562.         ).fill(true);
    
  563.         state.unresolvedSuspenseEvents.forEach(({depth}) => {
    
  564.           availableDepths[depth] = false;
    
  565.         });
    
  566. 
    
  567.         let depth = 0;
    
  568.         for (let i = 0; i < availableDepths.length; i++) {
    
  569.           if (availableDepths[i]) {
    
  570.             depth = i;
    
  571.             break;
    
  572.           }
    
  573.         }
    
  574. 
    
  575.         // TODO (timeline) Maybe we should calculate depth in post,
    
  576.         // so unresolved Suspense requests don't take up space.
    
  577.         // We can't know if they'll be resolved or not at this point.
    
  578.         // We'll just give them a default (fake) duration width.
    
  579. 
    
  580.         const suspenseEvent = {
    
  581.           componentName,
    
  582.           depth,
    
  583.           duration: null,
    
  584.           id,
    
  585.           phase: ((phase: any): Phase),
    
  586.           promiseName: promiseName || null,
    
  587.           resolution: 'unresolved',
    
  588.           timestamp: startTime,
    
  589.           type: 'suspense',
    
  590.           warning: null,
    
  591.         };
    
  592. 
    
  593.         if (phase === 'update') {
    
  594.           // If a component suspended during an update, we should verify that it was during a transition.
    
  595.           // We need the lane metadata to verify this though.
    
  596.           // Since that data is only logged during commit, we may not have it yet.
    
  597.           // Store these events for post-processing then.
    
  598.           state.potentialSuspenseEventsOutsideOfTransition.push([
    
  599.             suspenseEvent,
    
  600.             lanes,
    
  601.           ]);
    
  602.         }
    
  603. 
    
  604.         currentProfilerData.suspenseEvents.push(suspenseEvent);
    
  605.         state.unresolvedSuspenseEvents.set(id, suspenseEvent);
    
  606.       } else if (name.startsWith('--suspense-resolved-')) {
    
  607.         const [id] = name.slice(20).split('-');
    
  608.         const suspenseEvent = state.unresolvedSuspenseEvents.get(id);
    
  609.         if (suspenseEvent != null) {
    
  610.           state.unresolvedSuspenseEvents.delete(id);
    
  611. 
    
  612.           suspenseEvent.duration = startTime - suspenseEvent.timestamp;
    
  613.           suspenseEvent.resolution = 'resolved';
    
  614.         }
    
  615.       } else if (name.startsWith('--suspense-rejected-')) {
    
  616.         const [id] = name.slice(20).split('-');
    
  617.         const suspenseEvent = state.unresolvedSuspenseEvents.get(id);
    
  618.         if (suspenseEvent != null) {
    
  619.           state.unresolvedSuspenseEvents.delete(id);
    
  620. 
    
  621.           suspenseEvent.duration = startTime - suspenseEvent.timestamp;
    
  622.           suspenseEvent.resolution = 'rejected';
    
  623.         }
    
  624.       } else if (name.startsWith('--render-start-')) {
    
  625.         if (state.nextRenderShouldGenerateNewBatchID) {
    
  626.           state.nextRenderShouldGenerateNewBatchID = false;
    
  627.           state.batchUID = ((state.uidCounter++: any): BatchUID);
    
  628.         }
    
  629. 
    
  630.         // If this render is the result of a nested update, make a note of it.
    
  631.         // Once we're done processing events, we'll check to see if it was a long update and warn about it.
    
  632.         if (state.potentialLongNestedUpdate !== null) {
    
  633.           state.potentialLongNestedUpdates.push([
    
  634.             state.potentialLongNestedUpdate,
    
  635.             state.batchUID,
    
  636.           ]);
    
  637.           state.potentialLongNestedUpdate = null;
    
  638.         }
    
  639. 
    
  640.         const [laneBitmaskString] = name.slice(15).split('-');
    
  641. 
    
  642.         throwIfIncomplete('render', state.measureStack);
    
  643.         if (getLastType(state.measureStack) !== 'render-idle') {
    
  644.           markWorkStarted(
    
  645.             'render-idle',
    
  646.             startTime,
    
  647.             getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  648.             currentProfilerData,
    
  649.             state,
    
  650.           );
    
  651.         }
    
  652.         markWorkStarted(
    
  653.           'render',
    
  654.           startTime,
    
  655.           getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  656.           currentProfilerData,
    
  657.           state,
    
  658.         );
    
  659. 
    
  660.         for (let i = 0; i < state.nativeEventStack.length; i++) {
    
  661.           const nativeEvent = state.nativeEventStack[i];
    
  662.           const stopTime = nativeEvent.timestamp + nativeEvent.duration;
    
  663. 
    
  664.           // If React work was scheduled during an event handler, and the event had a long duration,
    
  665.           // it might be because the React render was long and stretched the event.
    
  666.           // It might also be that the React work was short and that something else stretched the event.
    
  667.           // Make a note of this event for now and we'll examine the batch of React render work later.
    
  668.           // (We can't know until we're done processing the React update anyway.)
    
  669.           if (stopTime > startTime) {
    
  670.             state.potentialLongEvents.push([nativeEvent, state.batchUID]);
    
  671.           }
    
  672.         }
    
  673.       } else if (
    
  674.         name.startsWith('--render-stop') ||
    
  675.         name.startsWith('--render-yield')
    
  676.       ) {
    
  677.         markWorkCompleted(
    
  678.           'render',
    
  679.           startTime,
    
  680.           currentProfilerData,
    
  681.           state.measureStack,
    
  682.         );
    
  683.       } else if (name.startsWith('--commit-start-')) {
    
  684.         state.nextRenderShouldGenerateNewBatchID = true;
    
  685.         const [laneBitmaskString] = name.slice(15).split('-');
    
  686. 
    
  687.         markWorkStarted(
    
  688.           'commit',
    
  689.           startTime,
    
  690.           getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  691.           currentProfilerData,
    
  692.           state,
    
  693.         );
    
  694.       } else if (name.startsWith('--commit-stop')) {
    
  695.         markWorkCompleted(
    
  696.           'commit',
    
  697.           startTime,
    
  698.           currentProfilerData,
    
  699.           state.measureStack,
    
  700.         );
    
  701.         markWorkCompleted(
    
  702.           'render-idle',
    
  703.           startTime,
    
  704.           currentProfilerData,
    
  705.           state.measureStack,
    
  706.         );
    
  707.       } else if (name.startsWith('--layout-effects-start-')) {
    
  708.         const [laneBitmaskString] = name.slice(23).split('-');
    
  709. 
    
  710.         markWorkStarted(
    
  711.           'layout-effects',
    
  712.           startTime,
    
  713.           getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  714.           currentProfilerData,
    
  715.           state,
    
  716.         );
    
  717.       } else if (name.startsWith('--layout-effects-stop')) {
    
  718.         markWorkCompleted(
    
  719.           'layout-effects',
    
  720.           startTime,
    
  721.           currentProfilerData,
    
  722.           state.measureStack,
    
  723.         );
    
  724.       } else if (name.startsWith('--passive-effects-start-')) {
    
  725.         const [laneBitmaskString] = name.slice(24).split('-');
    
  726. 
    
  727.         markWorkStarted(
    
  728.           'passive-effects',
    
  729.           startTime,
    
  730.           getLanesFromTransportDecimalBitmask(laneBitmaskString),
    
  731.           currentProfilerData,
    
  732.           state,
    
  733.         );
    
  734.       } else if (name.startsWith('--passive-effects-stop')) {
    
  735.         markWorkCompleted(
    
  736.           'passive-effects',
    
  737.           startTime,
    
  738.           currentProfilerData,
    
  739.           state.measureStack,
    
  740.         );
    
  741.       } else if (name.startsWith('--react-internal-module-start-')) {
    
  742.         const stackFrameStart = name.slice(30);
    
  743. 
    
  744.         if (!state.internalModuleStackStringSet.has(stackFrameStart)) {
    
  745.           state.internalModuleStackStringSet.add(stackFrameStart);
    
  746. 
    
  747.           const parsedStackFrameStart = parseStackFrame(stackFrameStart);
    
  748. 
    
  749.           state.internalModuleCurrentStackFrame = parsedStackFrameStart;
    
  750.         }
    
  751.       } else if (name.startsWith('--react-internal-module-stop-')) {
    
  752.         const stackFrameStop = name.slice(29);
    
  753. 
    
  754.         if (!state.internalModuleStackStringSet.has(stackFrameStop)) {
    
  755.           state.internalModuleStackStringSet.add(stackFrameStop);
    
  756. 
    
  757.           const parsedStackFrameStop = parseStackFrame(stackFrameStop);
    
  758. 
    
  759.           if (
    
  760.             parsedStackFrameStop !== null &&
    
  761.             state.internalModuleCurrentStackFrame !== null
    
  762.           ) {
    
  763.             const parsedStackFrameStart = state.internalModuleCurrentStackFrame;
    
  764. 
    
  765.             state.internalModuleCurrentStackFrame = null;
    
  766. 
    
  767.             const range = [parsedStackFrameStart, parsedStackFrameStop];
    
  768.             const ranges = currentProfilerData.internalModuleSourceToRanges.get(
    
  769.               parsedStackFrameStart.fileName,
    
  770.             );
    
  771.             if (ranges == null) {
    
  772.               currentProfilerData.internalModuleSourceToRanges.set(
    
  773.                 parsedStackFrameStart.fileName,
    
  774.                 [range],
    
  775.               );
    
  776.             } else {
    
  777.               ranges.push(range);
    
  778.             }
    
  779.           }
    
  780.         }
    
  781.       } else if (ph === 'R' || ph === 'n') {
    
  782.         // User Timing mark
    
  783.         currentProfilerData.otherUserTimingMarks.push({
    
  784.           name,
    
  785.           timestamp: startTime,
    
  786.         });
    
  787.       } else if (ph === 'b') {
    
  788.         // TODO: Begin user timing measure
    
  789.       } else if (ph === 'e') {
    
  790.         // TODO: End user timing measure
    
  791.       } else if (ph === 'i' || ph === 'I') {
    
  792.         // Instant events.
    
  793.         // Note that the capital "I" is a deprecated value that exists in Chrome Canary traces.
    
  794.       } else {
    
  795.         throw new InvalidProfileError(
    
  796.           `Unrecognized event ${JSON.stringify(
    
  797.             event,
    
  798.           )}! This is likely a bug in this profiler tool.`,
    
  799.         );
    
  800.       }
    
  801.       break;
    
  802.   }
    
  803. }
    
  804. 
    
  805. function assertNoOverlappingComponentMeasure(state: ProcessorState) {
    
  806.   if (state.currentReactComponentMeasure !== null) {
    
  807.     console.error(
    
  808.       'Component measure started while another measure in progress:',
    
  809.       state.currentReactComponentMeasure,
    
  810.     );
    
  811.   }
    
  812. }
    
  813. 
    
  814. function assertCurrentComponentMeasureType(
    
  815.   state: ProcessorState,
    
  816.   type: ReactComponentMeasureType,
    
  817. ): void {
    
  818.   if (state.currentReactComponentMeasure === null) {
    
  819.     console.error(
    
  820.       `Component measure type "${type}" stopped while no measure was in progress`,
    
  821.     );
    
  822.   } else if (state.currentReactComponentMeasure.type !== type) {
    
  823.     console.error(
    
  824.       `Component measure type "${type}" stopped while type ${state.currentReactComponentMeasure.type} in progress`,
    
  825.     );
    
  826.   }
    
  827. }
    
  828. 
    
  829. function processReactComponentMeasure(
    
  830.   name: string,
    
  831.   startTime: Milliseconds,
    
  832.   currentProfilerData: TimelineData,
    
  833.   state: ProcessorState,
    
  834. ): void {
    
  835.   if (name.startsWith('--component-render-start-')) {
    
  836.     const [componentName] = name.slice(25).split('-');
    
  837. 
    
  838.     assertNoOverlappingComponentMeasure(state);
    
  839. 
    
  840.     state.currentReactComponentMeasure = {
    
  841.       componentName,
    
  842.       timestamp: startTime,
    
  843.       duration: 0,
    
  844.       type: 'render',
    
  845.       warning: null,
    
  846.     };
    
  847.   } else if (name === '--component-render-stop') {
    
  848.     assertCurrentComponentMeasureType(state, 'render');
    
  849. 
    
  850.     if (state.currentReactComponentMeasure !== null) {
    
  851.       const componentMeasure = state.currentReactComponentMeasure;
    
  852.       componentMeasure.duration = startTime - componentMeasure.timestamp;
    
  853. 
    
  854.       state.currentReactComponentMeasure = null;
    
  855. 
    
  856.       currentProfilerData.componentMeasures.push(componentMeasure);
    
  857.     }
    
  858.   } else if (name.startsWith('--component-layout-effect-mount-start-')) {
    
  859.     const [componentName] = name.slice(38).split('-');
    
  860. 
    
  861.     assertNoOverlappingComponentMeasure(state);
    
  862. 
    
  863.     state.currentReactComponentMeasure = {
    
  864.       componentName,
    
  865.       timestamp: startTime,
    
  866.       duration: 0,
    
  867.       type: 'layout-effect-mount',
    
  868.       warning: null,
    
  869.     };
    
  870.   } else if (name === '--component-layout-effect-mount-stop') {
    
  871.     assertCurrentComponentMeasureType(state, 'layout-effect-mount');
    
  872. 
    
  873.     if (state.currentReactComponentMeasure !== null) {
    
  874.       const componentMeasure = state.currentReactComponentMeasure;
    
  875.       componentMeasure.duration = startTime - componentMeasure.timestamp;
    
  876. 
    
  877.       state.currentReactComponentMeasure = null;
    
  878. 
    
  879.       currentProfilerData.componentMeasures.push(componentMeasure);
    
  880.     }
    
  881.   } else if (name.startsWith('--component-layout-effect-unmount-start-')) {
    
  882.     const [componentName] = name.slice(40).split('-');
    
  883. 
    
  884.     assertNoOverlappingComponentMeasure(state);
    
  885. 
    
  886.     state.currentReactComponentMeasure = {
    
  887.       componentName,
    
  888.       timestamp: startTime,
    
  889.       duration: 0,
    
  890.       type: 'layout-effect-unmount',
    
  891.       warning: null,
    
  892.     };
    
  893.   } else if (name === '--component-layout-effect-unmount-stop') {
    
  894.     assertCurrentComponentMeasureType(state, 'layout-effect-unmount');
    
  895. 
    
  896.     if (state.currentReactComponentMeasure !== null) {
    
  897.       const componentMeasure = state.currentReactComponentMeasure;
    
  898.       componentMeasure.duration = startTime - componentMeasure.timestamp;
    
  899. 
    
  900.       state.currentReactComponentMeasure = null;
    
  901. 
    
  902.       currentProfilerData.componentMeasures.push(componentMeasure);
    
  903.     }
    
  904.   } else if (name.startsWith('--component-passive-effect-mount-start-')) {
    
  905.     const [componentName] = name.slice(39).split('-');
    
  906. 
    
  907.     assertNoOverlappingComponentMeasure(state);
    
  908. 
    
  909.     state.currentReactComponentMeasure = {
    
  910.       componentName,
    
  911.       timestamp: startTime,
    
  912.       duration: 0,
    
  913.       type: 'passive-effect-mount',
    
  914.       warning: null,
    
  915.     };
    
  916.   } else if (name === '--component-passive-effect-mount-stop') {
    
  917.     assertCurrentComponentMeasureType(state, 'passive-effect-mount');
    
  918. 
    
  919.     if (state.currentReactComponentMeasure !== null) {
    
  920.       const componentMeasure = state.currentReactComponentMeasure;
    
  921.       componentMeasure.duration = startTime - componentMeasure.timestamp;
    
  922. 
    
  923.       state.currentReactComponentMeasure = null;
    
  924. 
    
  925.       currentProfilerData.componentMeasures.push(componentMeasure);
    
  926.     }
    
  927.   } else if (name.startsWith('--component-passive-effect-unmount-start-')) {
    
  928.     const [componentName] = name.slice(41).split('-');
    
  929. 
    
  930.     assertNoOverlappingComponentMeasure(state);
    
  931. 
    
  932.     state.currentReactComponentMeasure = {
    
  933.       componentName,
    
  934.       timestamp: startTime,
    
  935.       duration: 0,
    
  936.       type: 'passive-effect-unmount',
    
  937.       warning: null,
    
  938.     };
    
  939.   } else if (name === '--component-passive-effect-unmount-stop') {
    
  940.     assertCurrentComponentMeasureType(state, 'passive-effect-unmount');
    
  941. 
    
  942.     if (state.currentReactComponentMeasure !== null) {
    
  943.       const componentMeasure = state.currentReactComponentMeasure;
    
  944.       componentMeasure.duration = startTime - componentMeasure.timestamp;
    
  945. 
    
  946.       state.currentReactComponentMeasure = null;
    
  947. 
    
  948.       currentProfilerData.componentMeasures.push(componentMeasure);
    
  949.     }
    
  950.   }
    
  951. }
    
  952. 
    
  953. function preprocessFlamechart(rawData: TimelineEvent[]): Flamechart {
    
  954.   let parsedData;
    
  955.   try {
    
  956.     parsedData = importFromChromeTimeline(rawData, 'react-devtools');
    
  957.   } catch (error) {
    
  958.     // Assume any Speedscope errors are caused by bad profiles
    
  959.     const errorToRethrow = new InvalidProfileError(error.message);
    
  960.     errorToRethrow.stack = error.stack;
    
  961.     throw errorToRethrow;
    
  962.   }
    
  963. 
    
  964.   const profile = parsedData.profiles[0]; // TODO: Choose the main CPU thread only
    
  965. 
    
  966.   const speedscopeFlamechart = new SpeedscopeFlamechart({
    
  967.     // $FlowFixMe[method-unbinding]
    
  968.     getTotalWeight: profile.getTotalWeight.bind(profile),
    
  969.     // $FlowFixMe[method-unbinding]
    
  970.     forEachCall: profile.forEachCall.bind(profile),
    
  971.     // $FlowFixMe[method-unbinding]
    
  972.     formatValue: profile.formatValue.bind(profile),
    
  973.     getColorBucketForFrame: () => 0,
    
  974.   });
    
  975. 
    
  976.   const flamechart: Flamechart = speedscopeFlamechart.getLayers().map(layer =>
    
  977.     layer.map(
    
  978.       ({
    
  979.         start,
    
  980.         end,
    
  981.         node: {
    
  982.           frame: {name, file, line, col},
    
  983.         },
    
  984.       }) => ({
    
  985.         name,
    
  986.         timestamp: start / 1000,
    
  987.         duration: (end - start) / 1000,
    
  988.         scriptUrl: file,
    
  989.         locationLine: line,
    
  990.         locationColumn: col,
    
  991.       }),
    
  992.     ),
    
  993.   );
    
  994. 
    
  995.   return flamechart;
    
  996. }
    
  997. 
    
  998. function parseStackFrame(stackFrame: string): ErrorStackFrame | null {
    
  999.   const error = new Error();
    
  1000.   error.stack = stackFrame;
    
  1001. 
    
  1002.   const frames = ErrorStackParser.parse(error);
    
  1003. 
    
  1004.   return frames.length === 1 ? frames[0] : null;
    
  1005. }
    
  1006. 
    
  1007. export default async function preprocessData(
    
  1008.   timeline: TimelineEvent[],
    
  1009. ): Promise<TimelineData> {
    
  1010.   const flamechart = preprocessFlamechart(timeline);
    
  1011. 
    
  1012.   const laneToReactMeasureMap: Map<ReactLane, Array<ReactMeasure>> = new Map();
    
  1013.   for (let lane: ReactLane = 0; lane < REACT_TOTAL_NUM_LANES; lane++) {
    
  1014.     laneToReactMeasureMap.set(lane, []);
    
  1015.   }
    
  1016. 
    
  1017.   const profilerData: TimelineData = {
    
  1018.     batchUIDToMeasuresMap: new Map(),
    
  1019.     componentMeasures: [],
    
  1020.     duration: 0,
    
  1021.     flamechart,
    
  1022.     internalModuleSourceToRanges: new Map(),
    
  1023.     laneToLabelMap: new Map(),
    
  1024.     laneToReactMeasureMap,
    
  1025.     nativeEvents: [],
    
  1026.     networkMeasures: [],
    
  1027.     otherUserTimingMarks: [],
    
  1028.     reactVersion: null,
    
  1029.     schedulingEvents: [],
    
  1030.     snapshots: [],
    
  1031.     snapshotHeight: 0,
    
  1032.     startTime: 0,
    
  1033.     suspenseEvents: [],
    
  1034.     thrownErrors: [],
    
  1035.   };
    
  1036. 
    
  1037.   // Sort `timeline`. JSON Array Format trace events need not be ordered. See:
    
  1038.   // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.f2f0yd51wi15
    
  1039.   timeline = timeline.filter(Boolean).sort((a, b) => (a.ts > b.ts ? 1 : -1));
    
  1040. 
    
  1041.   // Events displayed in flamechart have timestamps relative to the profile
    
  1042.   // event's startTime. Source: https://github.com/v8/v8/blob/44bd8fd7/src/inspector/js_protocol.json#L1486
    
  1043.   //
    
  1044.   // We'll thus expect there to be a 'Profile' event; if there is not one, we
    
  1045.   // can deduce that there are no flame chart events. As we expect React
    
  1046.   // scheduling profiling user timing marks to be recorded together with browser
    
  1047.   // flame chart events, we can futher deduce that the data is invalid and we
    
  1048.   // don't bother finding React events.
    
  1049.   const indexOfProfileEvent = timeline.findIndex(
    
  1050.     event => event.name === 'Profile',
    
  1051.   );
    
  1052.   if (indexOfProfileEvent === -1) {
    
  1053.     return profilerData;
    
  1054.   }
    
  1055. 
    
  1056.   // Use Profile event's `startTime` as the start time to align with flame chart.
    
  1057.   // TODO: Remove assumption that there'll only be 1 'Profile' event. If this
    
  1058.   // assumption does not hold, the chart may start at the wrong time.
    
  1059.   profilerData.startTime = timeline[indexOfProfileEvent].args.data.startTime;
    
  1060.   profilerData.duration =
    
  1061.     (timeline[timeline.length - 1].ts - profilerData.startTime) / 1000;
    
  1062. 
    
  1063.   const state: ProcessorState = {
    
  1064.     asyncProcessingPromises: [],
    
  1065.     batchUID: 0,
    
  1066.     currentReactComponentMeasure: null,
    
  1067.     internalModuleCurrentStackFrame: null,
    
  1068.     internalModuleStackStringSet: new Set(),
    
  1069.     measureStack: [],
    
  1070.     nativeEventStack: [],
    
  1071.     nextRenderShouldGenerateNewBatchID: true,
    
  1072.     potentialLongEvents: [],
    
  1073.     potentialLongNestedUpdate: null,
    
  1074.     potentialLongNestedUpdates: [],
    
  1075.     potentialSuspenseEventsOutsideOfTransition: [],
    
  1076.     requestIdToNetworkMeasureMap: new Map(),
    
  1077.     uidCounter: 0,
    
  1078.     unresolvedSuspenseEvents: new Map(),
    
  1079.   };
    
  1080. 
    
  1081.   timeline.forEach(event => processTimelineEvent(event, profilerData, state));
    
  1082. 
    
  1083.   if (profilerVersion === null) {
    
  1084.     if (
    
  1085.       profilerData.schedulingEvents.length === 0 &&
    
  1086.       profilerData.batchUIDToMeasuresMap.size === 0
    
  1087.     ) {
    
  1088.       // No profiler version could indicate data was logged using an older build of React,
    
  1089.       // before an explicitly profiler version was included in the logging data.
    
  1090.       // But it could also indicate that the website was either not using React, or using a production build.
    
  1091.       // The easiest way to check for this case is to see if the data contains any scheduled updates or render work.
    
  1092.       throw new InvalidProfileError(
    
  1093.         'No React marks were found in the provided profile.' +
    
  1094.           ' Please provide profiling data from an React application running in development or profiling mode.',
    
  1095.       );
    
  1096.     }
    
  1097. 
    
  1098.     throw new InvalidProfileError(
    
  1099.       `This version of profiling data is not supported by the current profiler.`,
    
  1100.     );
    
  1101.   }
    
  1102. 
    
  1103.   // Validate that all events and measures are complete
    
  1104.   const {measureStack} = state;
    
  1105.   if (measureStack.length > 0) {
    
  1106.     console.error('Incomplete events or measures', measureStack);
    
  1107.   }
    
  1108. 
    
  1109.   // Check for warnings.
    
  1110.   state.potentialLongEvents.forEach(([nativeEvent, batchUID]) => {
    
  1111.     // See how long the subsequent batch of React work was.
    
  1112.     // Ignore any work that was already started.
    
  1113.     const [startTime, stopTime] = getBatchRange(
    
  1114.       batchUID,
    
  1115.       profilerData,
    
  1116.       nativeEvent.timestamp,
    
  1117.     );
    
  1118.     if (stopTime - startTime > NATIVE_EVENT_DURATION_THRESHOLD) {
    
  1119.       nativeEvent.warning = WARNING_STRINGS.LONG_EVENT_HANDLER;
    
  1120.     }
    
  1121.   });
    
  1122.   state.potentialLongNestedUpdates.forEach(([schedulingEvent, batchUID]) => {
    
  1123.     // See how long the subsequent batch of React work was.
    
  1124.     const [startTime, stopTime] = getBatchRange(batchUID, profilerData);
    
  1125.     if (stopTime - startTime > NESTED_UPDATE_DURATION_THRESHOLD) {
    
  1126.       // Don't warn about transition updates scheduled during the commit phase.
    
  1127.       // e.g. useTransition, useDeferredValue
    
  1128.       // These are allowed to be long-running.
    
  1129.       if (
    
  1130.         !schedulingEvent.lanes.some(
    
  1131.           lane => profilerData.laneToLabelMap.get(lane) === 'Transition',
    
  1132.         )
    
  1133.       ) {
    
  1134.         // FIXME: This warning doesn't account for "nested updates" that are
    
  1135.         // spawned by useDeferredValue. Disabling temporarily until we figure
    
  1136.         // out the right way to handle this.
    
  1137.         // schedulingEvent.warning = WARNING_STRINGS.NESTED_UPDATE;
    
  1138.       }
    
  1139.     }
    
  1140.   });
    
  1141.   state.potentialSuspenseEventsOutsideOfTransition.forEach(
    
  1142.     ([suspenseEvent, lanes]) => {
    
  1143.       // HACK This is a bit gross but the numeric lane value might change between render versions.
    
  1144.       if (
    
  1145.         !lanes.some(
    
  1146.           lane => profilerData.laneToLabelMap.get(lane) === 'Transition',
    
  1147.         )
    
  1148.       ) {
    
  1149.         suspenseEvent.warning = WARNING_STRINGS.SUSPEND_DURING_UPDATE;
    
  1150.       }
    
  1151.     },
    
  1152.   );
    
  1153. 
    
  1154.   // Wait for any async processing to complete before returning.
    
  1155.   // Since processing is done in a worker, async work must complete before data is serialized and returned.
    
  1156.   await Promise.all(state.asyncProcessingPromises);
    
  1157. 
    
  1158.   // Now that all images have been loaded, let's figure out the display size we're going to use for our thumbnails:
    
  1159.   // both the ones rendered to the canvas and the ones shown on hover.
    
  1160.   if (profilerData.snapshots.length > 0) {
    
  1161.     // NOTE We assume a static window size here, which is not necessarily true but should be for most cases.
    
  1162.     // Regardless, Chrome also sets a single size/ratio and stick with it- so we'll do the same.
    
  1163.     const snapshot = profilerData.snapshots[0];
    
  1164. 
    
  1165.     profilerData.snapshotHeight = Math.min(
    
  1166.       snapshot.height,
    
  1167.       SNAPSHOT_MAX_HEIGHT,
    
  1168.     );
    
  1169.   }
    
  1170. 
    
  1171.   return profilerData;
    
  1172. }