From 120e87b86e634992a410ec4e37494ba7fc742b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 9 Dec 2022 12:15:42 -0800 Subject: [PATCH] Show warning when native module for performance marks and measures is not available Summary: We're currently showing warnings when we call `performanceObserver.observe` and the native module for performance observers isn't available, but we don't do the same for `performance.mark`, `performance.measure`, etc. This adds the warning in those cases. Changelog: [internal] Reviewed By: rshest Differential Revision: D41872270 fbshipit-source-id: d720580b930550f27c827a58243579c42a4f6da9 --- Libraries/WebPerformance/Performance.js | 50 +++++++++++++++++++------ 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Libraries/WebPerformance/Performance.js b/Libraries/WebPerformance/Performance.js index 6c8358c66b4..0f05de293c2 100644 --- a/Libraries/WebPerformance/Performance.js +++ b/Libraries/WebPerformance/Performance.js @@ -10,6 +10,7 @@ import type {HighResTimeStamp} from './PerformanceObserver'; +import warnOnce from '../Utilities/warnOnce'; import NativePerformance from './NativePerformance'; import {PerformanceEntry} from './PerformanceObserver'; @@ -72,6 +73,13 @@ export class PerformanceMeasure extends PerformanceEntry { } } +function warnNoNativePerformance() { + warnOnce( + 'missing-native-performance', + 'Missing native implementation of Performance', + ); +} + /** * Partial implementation of the Performance interface for RN, * corresponding to the standard in @@ -83,12 +91,23 @@ export default class Performance { markOptions?: PerformanceMarkOptions, ): PerformanceMark { const mark = new PerformanceMark(markName, markOptions); - NativePerformance?.mark?.(markName, mark.startTime, mark.duration); + + if (NativePerformance?.mark) { + NativePerformance.mark(markName, mark.startTime, mark.duration); + } else { + warnNoNativePerformance(); + } + return mark; } clearMarks(markName?: string): void { - NativePerformance?.clearMarks?.(markName); + if (!NativePerformance?.clearMarks) { + warnNoNativePerformance(); + return; + } + + NativePerformance.clearMarks(markName); } measure( @@ -144,20 +163,29 @@ export default class Performance { const measure = new PerformanceMeasure(measureName, options); - NativePerformance?.measure?.( - measureName, - startTime, - endTime, - duration, - startMarkName, - endMarkName, - ); + if (NativePerformance?.measure) { + NativePerformance.measure( + measureName, + startTime, + endTime, + duration, + startMarkName, + endMarkName, + ); + } else { + warnNoNativePerformance(); + } return measure; } clearMeasures(measureName?: string): void { - NativePerformance?.clearMeasures?.(measureName); + if (!NativePerformance?.clearMeasures) { + warnNoNativePerformance(); + return; + } + + NativePerformance.clearMeasures(measureName); } now(): HighResTimeStamp {