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 memoize from 'memoize-one';
    
  11. 
    
  12. import type {
    
  13.   BatchUID,
    
  14.   Milliseconds,
    
  15.   ReactMeasure,
    
  16.   TimelineData,
    
  17. } from '../types';
    
  18. 
    
  19. function unmemoizedGetBatchRange(
    
  20.   batchUID: BatchUID,
    
  21.   data: TimelineData,
    
  22.   minStartTime?: number = 0,
    
  23. ): [Milliseconds, Milliseconds] {
    
  24.   const measures = data.batchUIDToMeasuresMap.get(batchUID);
    
  25.   if (measures == null || measures.length === 0) {
    
  26.     throw Error(`Could not find measures with batch UID "${batchUID}"`);
    
  27.   }
    
  28. 
    
  29.   const lastMeasure = ((measures[measures.length - 1]: any): ReactMeasure);
    
  30.   const stopTime = lastMeasure.timestamp + lastMeasure.duration;
    
  31. 
    
  32.   if (stopTime < minStartTime) {
    
  33.     return [0, 0];
    
  34.   }
    
  35. 
    
  36.   let startTime = minStartTime;
    
  37.   for (let index = 0; index < measures.length; index++) {
    
  38.     const measure = measures[index];
    
  39.     if (measure.timestamp >= minStartTime) {
    
  40.       startTime = measure.timestamp;
    
  41.       break;
    
  42.     }
    
  43.   }
    
  44. 
    
  45.   return [startTime, stopTime];
    
  46. }
    
  47. 
    
  48. export const getBatchRange: (
    
  49.   batchUID: BatchUID,
    
  50.   data: TimelineData,
    
  51.   minStartTime?: number,
    
  52. ) => [Milliseconds, Milliseconds] = memoize(unmemoizedGetBatchRange);