Files
react/packages/react-devtools-timeline/src/utils/getBatchRange.js
T
Andrew Clark 9cdf8a99ed [Codemod] Update copyright header to Meta (#25315)
* Facebook -> Meta in copyright

rg --files | xargs sed -i 's#Copyright (c) Facebook, Inc. and its affiliates.#Copyright (c) Meta Platforms, Inc. and affiliates.#g'

* Manual tweaks
2022-10-18 11:19:24 -04:00

53 lines
1.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import memoize from 'memoize-one';
import type {
BatchUID,
Milliseconds,
ReactMeasure,
TimelineData,
} from '../types';
function unmemoizedGetBatchRange(
batchUID: BatchUID,
data: TimelineData,
minStartTime?: number = 0,
): [Milliseconds, Milliseconds] {
const measures = data.batchUIDToMeasuresMap.get(batchUID);
if (measures == null || measures.length === 0) {
throw Error(`Could not find measures with batch UID "${batchUID}"`);
}
const lastMeasure = ((measures[measures.length - 1]: any): ReactMeasure);
const stopTime = lastMeasure.timestamp + lastMeasure.duration;
if (stopTime < minStartTime) {
return [0, 0];
}
let startTime = minStartTime;
for (let index = 0; index < measures.length; index++) {
const measure = measures[index];
if (measure.timestamp >= minStartTime) {
startTime = measure.timestamp;
break;
}
}
return [startTime, stopTime];
}
export const getBatchRange: (
batchUID: BatchUID,
data: TimelineData,
minStartTime?: number,
) => [Milliseconds, Milliseconds] = memoize(unmemoizedGetBatchRange);