mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
cf194aebfe
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36152 [Changelog][Internal] By [the W3C standard](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver/observe), `PerformanceObserver.observer` can optionally take a `durationThreshold` option, so that only entries with duration larger than the threshold are reported. This diff adds support for this on the RN side, as well as unit tests for this feature on the JS side. NOTE: The standard suggests that default value for this is 104s. I left it at 0 for now, as for the RN use cases t may be to too high (needs discussion). Reviewed By: rubennorte Differential Revision: D43154319 fbshipit-source-id: 0f9d435506f48d8e8521e408211347e8391d22fc
83 lines
2.3 KiB
JavaScript
83 lines
2.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 strict
|
|
* @format
|
|
*/
|
|
|
|
import type {
|
|
GetPendingEntriesResult,
|
|
RawPerformanceEntry,
|
|
RawPerformanceEntryType,
|
|
Spec as NativePerformanceObserver,
|
|
} from '../NativePerformanceObserver';
|
|
|
|
import {RawPerformanceEntryTypeValues} from '../RawPerformanceEntry';
|
|
|
|
const reportingType: Set<RawPerformanceEntryType> = new Set();
|
|
const eventCounts: Map<string, number> = new Map();
|
|
const durationThresholds: Map<RawPerformanceEntryType, number> = new Map();
|
|
let entries: Array<RawPerformanceEntry> = [];
|
|
let onPerformanceEntryCallback: ?() => void;
|
|
|
|
const NativePerformanceObserverMock: NativePerformanceObserver = {
|
|
startReporting: (entryType: RawPerformanceEntryType) => {
|
|
reportingType.add(entryType);
|
|
},
|
|
|
|
stopReporting: (entryType: RawPerformanceEntryType) => {
|
|
reportingType.delete(entryType);
|
|
durationThresholds.delete(entryType);
|
|
},
|
|
|
|
popPendingEntries: (): GetPendingEntriesResult => {
|
|
const res = entries;
|
|
entries = [];
|
|
return {
|
|
droppedEntriesCount: 0,
|
|
entries: res,
|
|
};
|
|
},
|
|
|
|
setOnPerformanceEntryCallback: (callback?: () => void) => {
|
|
onPerformanceEntryCallback = callback;
|
|
},
|
|
|
|
logRawEntry: (entry: RawPerformanceEntry) => {
|
|
if (reportingType.has(entry.entryType)) {
|
|
const durationThreshold = durationThresholds.get(entry.entryType);
|
|
if (
|
|
durationThreshold !== undefined &&
|
|
entry.duration < durationThreshold
|
|
) {
|
|
return;
|
|
}
|
|
entries.push(entry);
|
|
// $FlowFixMe[incompatible-call]
|
|
global.queueMicrotask(() => {
|
|
// We want to emulate the way it's done in native (i.e. async/batched)
|
|
onPerformanceEntryCallback?.();
|
|
});
|
|
}
|
|
if (entry.entryType === RawPerformanceEntryTypeValues.EVENT) {
|
|
eventCounts.set(entry.name, (eventCounts.get(entry.name) ?? 0) + 1);
|
|
}
|
|
},
|
|
|
|
getEventCounts: (): $ReadOnlyArray<[string, number]> => {
|
|
return Array.from(eventCounts.entries());
|
|
},
|
|
|
|
setDurationThreshold: (
|
|
entryType: RawPerformanceEntryType,
|
|
durationThreshold: number,
|
|
) => {
|
|
durationThresholds.set(entryType, durationThreshold);
|
|
},
|
|
};
|
|
|
|
export default NativePerformanceObserverMock;
|